【问题标题】:NullPointerException checkbox sharedpreferencesNullPointerException 复选框共享首选项
【发布时间】:2011-12-18 01:39:57
【问题描述】:

我正在尝试使用 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" />

等等……

【问题讨论】:

    标签: android nullpointerexception sharedpreferences


    【解决方案1】:

    您需要再次获取SharedPreferences。此时mPrefs 也可以是空对象。也尝试恢复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 :)
    

    【讨论】:

    • 您好 Nikola,谢谢您的回复,您介意给我一些更正代码吗?非常感谢。
    • 嗨 Nikola,感谢新代码,我应用了它,但我仍然在第 465 行收到 NullException:mCheckBox.setChecked(mPrefs.getBoolean(PREF_BOOL, false));我想很难不破解。
    • 复选框的ID是否正确?似乎 mPrefs 不为空。所以 mCheckBox 肯定是空的。
    • 复选框位于另一个名为 layout 的 xml 文件中。应该没问题。 mCheckBox = (CheckBox)findViewById(R.id.nieuwbel); // 在布局 xml 中 Im struggling with this for some days now but i cant 得到解决方案。我包含布局文件的代码,也许它有帮助。如果您能提供帮助,那就太好了。
    • Owww...这是另一个问题。如果复选框位于另一个布局(另一个 xml)中,那么您需要扩展该 XML...我将再次编辑我的答案。
    【解决方案2】:

    几个提示:

    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);
    

    【讨论】:

      猜你喜欢
      • 2012-09-08
      • 2016-12-26
      • 1970-01-01
      • 2016-09-13
      • 2014-10-26
      • 2016-12-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多