0

I am trying to save the state of my checkbox using SharedPreferences. I thought I had the code correct as it is not showing up any errors. I checked the LogCat for the cause and apparently there is something wrong with the onResume.

SuikodenFragment.java

public class SuikodenFragment extends Fragment implements OnItemClickListener {
public static final String suikodenprefs = "SuikodenPrefs" ;
ListView listView;
ArrayAdapter<Model> adapter;
List<Model> list = new ArrayList<Model>();

@Override
public  View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
    // Inflate the layout for this fragment
    View view =  inflater.inflate(R.layout.suikoden_main_activity1, container, false);
    listView = (ListView) view.findViewById(R.id.my_list);
    adapter = new SuikodenListAdapter(getActivity(),getModel());
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);
    //CheckBox lBox1 = (CheckBox) view.findViewById(R.id.check);

    return view;
}

@Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long arg3) {
    TextView label = (TextView) v.getTag(R.id.label);
CheckBox checkbox = (CheckBox) v.getTag(R.id.check);
Toast.makeText(v.getContext(), label.getText().toString()+" "+isCheckedOrNot(checkbox), Toast.LENGTH_LONG).show();
}

private String isCheckedOrNot(CheckBox checkbox) {
    if(checkbox.isChecked())
    return "is checked";
    else
    return "is not checked";
}

private void save(final boolean isChecked) {
    SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, 0);
    SharedPreferences.Editor editor = settings.edit();
    editor.putBoolean("check", isChecked);
    editor.commit();
}

private boolean load() { 
    SharedPreferences settings = getActivity().getSharedPreferences(suikodenprefs, 0);
    return settings.getBoolean("check", false);
}

@Override
public void onPause() {
    super.onPause();
    save(mCheckBox.isChecked());
}

@Override
public void onResume() {
    super.onResume();
    mCheckBox.setChecked(load());
}

There is also a ListView in this fragment, which I have put above because it is rather long. I have tried removing the onResume, the app starts up but when I click on a checkbox and move to another fragment (from navigation drawer) it crashes. Both crashes has the same thing in common.

onResume crash because of...

mCheckBox.setChecked(load());

When onResume is removed the crash happens because of onPause (specific line below) when switching between fragments...

save(mCheckBox.isChecked());

any ideas? Thanks!

EDIT As requested - the following

suikoden_activity_main1.xml

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

<ListView
android:id="@+id/my_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />

suikoden_row_1.xml

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

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:text="@+id/label"
android:textSize="25sp" >
</TextView>

<CheckBox
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:focusableInTouchMode="false" >
</CheckBox>

EDIT 2

This is where I got the SharedPreferences code from originally. How to save the state of an Android CheckBox when the users exits the application?

Community
  • 1
  • 1
theCreed
  • 55
  • 11
  • Take a look at `Activity` Lifecycle. `onResume` is called before `onCreateView`. The `NullPointerException` occurs because `lBox1` wasn't initialized. – Wakim Jun 17 '14 at 15:58
  • @Wakim its just the opposite – Illegal Argument Jun 17 '14 at 15:59
  • My mistake, i should write `onCreateView`, let me correct this. Thanks for advice. – Wakim Jun 17 '14 at 16:00
  • as far as I can see you are doing a mistake of referencing a checkbox to be used in SuikodenListAdapter by using in your fragment. Remove all lBox1 from your fragment code – Illegal Argument Jun 17 '14 at 16:20
  • @IllegalArgument I have updated the code. I originally took it from an answer given in another question. The link is in my first post. mCheckBox used to be lBox1, I just changed it to see if I could get it working. Now it is back to its original form, maybe you could help me? – theCreed Jun 17 '14 at 16:34

3 Answers3

0

The checkbox is null as it is not assigned to any view. Try this in your oncreate view. Make sure you have a checkbox in your xml file named lBox1:

lBox1 = view.findViewById(R.id.check);//be sure to reference correct layout or you will get class cast exception
Illegal Argument
  • 10,090
  • 2
  • 44
  • 61
  • hey, thanks for the answer, I implemented the line and for some reason it is saying i cannot convert from view to checkbox – theCreed Jun 17 '14 at 16:05
  • @theCreed post contents of this file R.layout.suikoden_main_activity1 – Illegal Argument Jun 17 '14 at 16:06
  • I have updated the code. I will say that lBox1 came from a tutorial I found somewhere. It can be changed to anything, im not fussed. Thanks! – theCreed Jun 17 '14 at 16:13
0

This is because CheckBox lBox1 is undefined. In your onCreate() add the following line:

lBox1 = (CheckBox) findViewById(R.id.check);

This should solve your problem.

android_Muncher
  • 1,057
  • 7
  • 14
0

onResume does not mean what you think it means.

It gets called at the beginning of the lifecycle as well. The only thing it's resuming is the main UI thread.

So it calls your load() method before it calls your save(...) method the first time you run your application.

Stephan Branczyk
  • 9,363
  • 2
  • 33
  • 49
  • ok thanks, what do I change it to? I did swap onResume and onPause for onStart and onStop...and it still crashed on the same lines – theCreed Jun 17 '14 at 16:37
  • Your onPause() is good. Don't change it. Certainly don't use onStop(). Just guard against having your sharedpreference not created when onResume() gets called. – Stephan Branczyk Jun 17 '14 at 16:45
  • ok thanks, and how do i guard against that? Sorry for all the questions but im beginner, thanks for your patience – theCreed Jun 17 '14 at 16:46