我正在尝试保存切换按钮的状态,但是崩溃了。我正在使用一个片段,因此我以这种方式创建了代码:
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;
}
---
---
日志:
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)
有人可以帮助我吗?
最佳答案
看来onOff
为空。
您尝试先设置onOff为null,然后尝试设置几行后再设置
onOff = (ToggleButton)v.findViewById(R.id.onoff);
在尝试访问onOff之前,请将此行上移。
if (tgpref = true)
也应该是if (tgpref == true)