NullPointerException casilla de verificación sharedpreferences
Intento guardar los valores de diferentes casillas de verificación ubicadas en un archivo layout.xml, usando las preferencias compartidas.
Pero cuando compilo el programa obtengo una NullPointerException, según Logcat, la causa está en la línea 463, marcada en mi código.
ntenté varias cosas, pero no puedo deshacerme de la excepción.
¿Alguien podría ayudarme por favor
La excepción de 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
Parte del código en mi programa principal xml:
En Crear y establecer valores:
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
The OnPause y 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(); }
El archivo de diseño con la casilla de verificación:
<?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" />
y así..