Question

I'm trying to save the state of a toggle button but i get a crash.. I'm using a fragment so i created the code in this way:

public class fragmentbutton extends Fragment {

    public ToggleButton onOff;
    @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                        Bundle savedInstanceState) {

            // Retrieving the currently selected item number
            int position = getArguments().getInt("position");
            // List of rivers
            String[] menus = getResources().getStringArray(R.array.menus);
            View v = inflater.inflate(R.layout.fragmentbutton, container, false);

             final SharedPreferences preferences = getActivity().getPreferences(getActivity().MODE_PRIVATE);
             boolean tgpref = preferences.getBoolean("tgpref", true);  //default is true
             if (tgpref = true) //if (tgpref) may be enough, not sure
             {
                 onOff.setChecked(true);
             }
             else
             {
                 onOff.setChecked(false);
             }

             retrieveFromDb();

                onOff = (ToggleButton)v.findViewById(R.id.onoff);
                onOff.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        if(isChecked == true){
                            getActivity().startService(new Intent(getActivity(),NotificationService.class));

                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putBoolean("tgpref", true); // value to store
                            editor.commit();
                        }else {
                            getActivity().stopService(new Intent(getActivity(),NotificationService.class));

                            SharedPreferences.Editor editor = preferences.edit();
                            editor.putBoolean("tgpref", false); // value to store
                            editor.commit();
                        }
                    }
                });
            return v;

        }
---
---

the logcat:

12-12 22:45:18.836: E/AndroidRuntime(23007): FATAL EXCEPTION: main
12-12 22:45:18.836: E/AndroidRuntime(23007): Process: com.dd.allinonesystempro, PID: 23007
12-12 22:45:18.836: E/AndroidRuntime(23007): java.lang.NullPointerException
12-12 22:45:18.836: E/AndroidRuntime(23007):    at com.dd.allinonesystempro.batteryfragment.onCreateView(batteryfragment.java:114)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.Fragment.performCreateView(Fragment.java:1700)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:890)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1062)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.BackStackRecord.run(BackStackRecord.java:684)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1447)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:443)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.os.Handler.handleCallback(Handler.java:733)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.os.Handler.dispatchMessage(Handler.java:95)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.os.Looper.loop(Looper.java:136)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at android.app.ActivityThread.main(ActivityThread.java:5017)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at java.lang.reflect.Method.invokeNative(Native Method)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at java.lang.reflect.Method.invoke(Method.java:515)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
12-12 22:45:18.836: E/AndroidRuntime(23007):    at dalvik.system.NativeStart.main(Native Method)

Anyone can help me?

Was it helpful?

Solution

It seems that onOff is null.

You are trying to set onOff first which is null and then after few lines trying to set

onOff = (ToggleButton)v.findViewById(R.id.onoff);

move this line up before you try to access onOff.

also if (tgpref = true) should be if (tgpref == true)

OTHER TIPS

You have to initialize your onOff view before calling setChecked() on it:

onOff = (ToggleButton)v.findViewById(R.id.onoff);

if (tgpref == true) //if (tgpref) may be enough, not sure
{
    onOff.setChecked(true);
}
else
{
    onOff.setChecked(false);
}

Let's try to change:

 if (tgpref = true)

to:

 if (tgpref == true)

To compare you have to use ==.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top