NullPointerException 复选框 sharedpreferences [英] NullPointerException checkbox sharedpreferences

查看:17
本文介绍了NullPointerException 复选框 sharedpreferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用共享首选项保存来自 layout.xml 文件中不同复选框的值.

I am trying to save the values from different checkboxes located in a layout.xml file, using sharedpreferences.

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

But when i compile the program i get a NullPointerException, according Logcat the cause is in line 463, marked in my code.

我尝试了几种方法,但无法摆脱异常.

I tried sevaral things, but i can`t get rid of the exception.

有人可以帮我吗?

来自 Logcat 的异常:

The Exception from 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中的部分代码:

Part of the code in my Main program 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:

The OnPause and 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();    }

带有复选框的布局文件:

The layout file with the checkbox:

<?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" />

等等...

推荐答案

您需要再次获取 SharedPreferences.此时 mPrefs 也可以是空对象.还可以尝试恢复 mCheckBox.如果活动从 onPause() 转到 onResume() 没有再次调用 onCreate() 并且您正确地实例化了两个对象,则会发生这种情况.

You need to get SharedPreferences again. At that point mPrefs can be also null object. Also try reinstatiating the mCheckBox. This happens if the activity went from onPause() to onResume() not calling onCreate() again where you have instatiation of both object properly.

    @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.

Edit 2: Inflating the other xml.

你把它放在 onResume() 中

You put this inside 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 :)

这篇关于NullPointerException 复选框 sharedpreferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆