SinTek Solutions SinTek Solutions - 2 months ago 11
Android Question

android: Checkbox null object reference

I'm sure I am doing something stupid wrong...

All I want to do is programatically check a checkbox. Here is that code:

CheckBox mycheck;
mycheck = (CheckBox) getView().findViewById(R.id.edu1415_3);
mycheck.setChecked(true);


I think the issue is doing it in a fragment. I just don't know how to fix it. I have tried in both onCreate methods but I get this error: Attempt to invoke virtual method 'void android.widget.CheckBox.setChecked(boolean)' on a null object reference

What am I missing? I appreciate the help!

Here are my files:

checklist1415_2.java:

package com.abc.paplanningforthefuturechecklist;

import android.content.Context;
import android.content.SharedPreferences;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import android.widget.CheckBox;
import android.widget.TextView;

public class checklist1415_2 extends AppCompatActivity {


private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checklist1415_2);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});

//checkTheBoxes();



}

public void onChecklistChecked(View view){
boolean checked = ((CheckBox) view).isChecked();
String checkedItem = getResources().getResourceEntryName(view.getId());
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
int checkVal = 1;
String pref_key = checkedItem;
if(checked){
editor.putInt(pref_key, checkVal);
editor.commit();
} else {
editor.putInt(pref_key, -1);
editor.commit();
}
System.out.println(pref_key);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_checklist1415_2, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();

if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}

public static class PlaceholderFragment extends Fragment {


private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View A = inflater.inflate(R.layout.edu1415, container, false);
View B = inflater.inflate(R.layout.employ1415, container, false);
View C = inflater.inflate(R.layout.comm1415, container, false);

switch (getArguments().getInt(ARG_SECTION_NUMBER)){
case 1:
return A;
case 2:
return B;
case 3:
return C;
}

return null;
}
}

public class SectionsPagerAdapter extends FragmentPagerAdapter {

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
// Show 3 total pages.
return 3;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}


edu1415.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edu1415_1"
android:text="@string/edu1415_1"
android:paddingTop="20dp" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edu1415_2"
android:text="@string/edu1415_2" />

<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/edu1415_3"
android:text="@string/edu1415_3"
android:onClick="onChecklistChecked"/>

</LinearLayout>


fragment_checklist1415_2.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.abc.paplanningforthefuturechecklist.checklist1415_2$PlaceholderFragment">

<TextView
android:id="@+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</RelativeLayout>

Answer

if you want to find a view in a Fragment, do it in onCreateView() like:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup Bundle savedInstanceState) {
    View root = inflater.inflate(R.layout.edu1415, container, false);
    CheckBox checkbox = (CheckBox) root.findViewById(R.id.checkbox);
    return root;
}

As getView() works only after onCreateView(), you can't use it inside onCreate(), onCreateView() methods of the fragment .