崩溃保存按钮切换状态

时间:2013-12-12 21:52:00

标签: java android sharedpreferences onclicklistener togglebutton

我正在尝试保存切换按钮的状态但是我遇到了崩溃..我正在使用片段,所以我以这种方式创建了代码:

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;

        }
---
---

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)

任何人都可以帮助我?

3 个答案:

答案 0 :(得分:1)

似乎onOff为空。

你试图首先设置onOff为null,然后在几行之后尝试设置

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

在尝试访问onOff之前移动此行。

if (tgpref = true)也应为if (tgpref == true)

答案 1 :(得分:1)

您必须在调用onOff之前初始化setChecked()视图:

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

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

答案 2 :(得分:0)

让我们尝试改变:

 if (tgpref = true)

为:

 if (tgpref == true)

要比较你必须使用==。