NullPointerException复选框sharedpreferences

时间:2011-10-30 01:21:18

标签: android nullpointerexception sharedpreferences

我正在尝试使用sharedpreferences保存layout.xml文件中不同复选框的值。

但是当我编译程序时,我得到一个NullPointerException,根据Logcat,原因在第463行,在我的代码中标记。

我试过了各种各样的东西,但是我无法摆脱异常。

有人可以帮助我吗?

Logcat的例外:

10-29 23:35:31.556: E/AndroidRuntime(3292): FATAL EXCEPTION: main 
10-29 23:35:31.556: E/AndroidRuntime(3292): java.lang.RuntimeException: Unable to resume activity {com.sencide/com.sencide.AndroidLogin}: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2241) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2256) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1789) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.access$1500(ActivityThread.java:123) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.os.Handler.dispatchMessage(Handler.java:99) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.os.Looper.loop(Looper.java:130) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.main(ActivityThread.java:3835) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invokeNative(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at java.lang.reflect.Method.invoke(Method.java:507) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at dalvik.system.NativeStart.main(Native Method) 
10-29 23:35:31.556: E/AndroidRuntime(3292): Caused by: java.lang.NullPointerException 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at com.sencide.AndroidLogin.onResume(AndroidLogin.java:463) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.Activity.performResume(Activity.java:3832) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2231) 
10-29 23:35:31.556: E/AndroidRuntime(3292):     ... 12 more 

我的主程序xml中的部分代码:

开创建并设定值:

public static final String PREFS_NAME = "SharedPrefsDemoPreferences"; 
public static final String PREF_BOOL = "PrefBool";
private SharedPreferences mPrefs;
private CheckBox mCheckBox;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    mPrefs = getSharedPreferences(PREFS_NAME, 0);
    mCheckBox = (CheckBox)findViewById(R.id.nieuwbel);// in layout.xml 

OnPause和OnResume:

@Override
protected void onResume() {      
    mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false)); <-- Line 463   
    super.onResume();}

@Override    
protected void onPause() { 
    Editor e = mPrefs.edit();
    e.putBoolean(PREF_BOOL, mCheckBox.isChecked());              
    e.commit();         
    Toast.makeText(this, "Settings Saved.", Toast.LENGTH_SHORT).show();        
    super.onPause();    }

带有复选框的布局文件:

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


<CheckBox
    android:id="@+id/nieuwbel"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Nieuw beltegoed" />


<CheckBox
    android:id="@+id/vorige"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Tegoed vorige periode" />

依旧......

2 个答案:

答案 0 :(得分:1)

您需要再次获得SharedPreferences。此时mPrefs也可以是null对象。同时尝试重新安排mCheckBox。如果活动从onPause()转到onResume()而没有再次调用onCreate(),那么就会发生这种情况。

编辑:

    @Override
    protected void onResume() {      
        mPrefs = getSharedPreferences(PREFS_NAME, 0);
         mCheckBox = (CheckBox)findViewById(R.id.nieuwbel);
        if(mPrefs!=null)  //check in case there are no prefs saved, due some reason
           mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false));
        else
            mCheckBox.setChecked(true); //here put default value if there are no preferences saved
        super.onResume();
}

编辑2:给另一个xml充气。

你把它放在onResume()

里面
LayoutInflater inflater = (LayoutInflater)getSystemService
      (Context.LAYOUT_INFLATER_SERVICE);
   View v = inflater.inflate(R.layout.layout, null); // you have the xml as a view from which you can extract the checkbox
   mCheckBox = (CheckBox)v.findViewById(R.id.nieuwbel);
   .... do the rest of the coding in the first edit :)

答案 1 :(得分:1)

一些提示:

1)确保在找到后检查它是否为空。

mCheckBox = (CheckBox)findViewById(R.id.nieuwbel);
Toast.makeText(this, "Is checkbox null? " + mCheckBox, Toast.LENGTH_SHORT).show(); 

2)用NullPointerException分隔该行,以便知道哪个变量为空。

boolean savedValue = mPrefs.getBoolean(PREF_BOOL, false);
mCheckBox.setChecked(savedValue);

如果mPrefs为null,那么您可能需要再次获取SharedPreferences。

mPrefs = getSharedPreferences(PREFS_NAME, 0);