如何在SharedPreferences中保存菜单项已检查状态?

时间:2016-06-01 12:19:44

标签: android nullpointerexception

在我的应用程序的菜单中,我有2个项目,其中一个是可检查的,我想使用SharedPreferences保存他的状态。 这是我的代码: 的 MainActivity

 MenuItem bubu1;
 boolean isChecked;

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

 bubu1.setChecked(getFromSP("cec1"));
  }

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.action_rate_us:

            break;
        case R.id.action_enab:
            saveInSp("cec1",isChecked);
            if (bubu1.isChecked()) {
                bubu1.setChecked(false);

            }else{

                bubu1.setChecked(true);
            }
            return true;
        default:
            break;
    }

    return super.onOptionsItemSelected(item);
}

private boolean getFromSP(String key){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    return preferences.getBoolean(key, false);
}

private void saveInSp(String key,boolean value){
    SharedPreferences preferences = getApplicationContext().getSharedPreferences("PROJECT_NAME", android.content.Context.MODE_PRIVATE);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putBoolean(key, value);
    editor.commit();
}


菜单xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_enab"
    android:title="@string/enab"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:checkable="true"
    android:checked="false"
    />

<item
    android:id="@+id/action_rate_us"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_rate_us"/>


以下是我得到的错误:

 FATAL EXCEPTION: main
 Process: dia.it.solutions.lagcleaner, PID: 6143
 java.lang.RuntimeException: Unable to start activity ComponentInfo{dia.it.solutions.lagcleaner/dia.it.solutions.lagcleaner.MainActivity}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2447)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507)
 at android.app.ActivityThread.access$900(ActivityThread.java:172)
 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
 at android.os.Handler.dispatchMessage(Handler.java:102)
 at android.os.Looper.loop(Looper.java:146)
 at android.app.ActivityThread.main(ActivityThread.java:5692)
 at java.lang.reflect.Method.invokeNative(Native Method)
 at java.lang.reflect.Method.invoke(Method.java:515)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at dia.it.solutions.lagcleaner.MainActivity.onCreate(MainActivity.java:95)
at android.app.Activity.performCreate(Activity.java:5541)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2507) 
  at android.app.ActivityThread.access$900(ActivityThread.java:172) 
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
   at android.os.Looper.loop(Looper.java:146) 
   at android.app.ActivityThread.main(ActivityThread.java:5692) 
   at java.lang.reflect.Method.invokeNative(Native Method) 


从错误中,我知道应用程序崩溃是因为MainActivity的第95行,这里是95行:

  

bubu1.setChecked(getFromSP(&#34; CEC1&#34));

任何想法为什么会这样?

2 个答案:

答案 0 :(得分:0)

bubu1null,您必须在使用前初始化视图。

答案 1 :(得分:0)

bubu1null onCreate中是 MenuItem bubu1; boolean isChecked; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_activity); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); bubu1 = menu.findItem(R.id.action_enab); bubu1.setChecked(getFromSP("cec1")); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_rate_us: break; case R.id.action_enab: if (bubu1.isChecked()) { bubu1.setChecked(false); saveInSp("cec1",false); }else{ bubu1.setChecked(true); saveInSp("cec1",true); } return true; default: break; } return super.onOptionsItemSelected(item); } ,因为它尚未初始化。 将您的代码更改为

  <fieldType name="timeframe_spatial"
       class="solr.SpatialRecursivePrefixTreeFieldType"
       geo="false"
       worldBounds="0 0 1999999999999 1999999999999"
       distErrPct="0"
       maxDistErr="1"
       units="degrees"/>