我正在尝试在我的 android 应用程序中保存一些首选项。但是当我试图获得一个共享偏好时,我得到了一个空指针异常。这是错误:
04-25 13:58:44.814: W/System.err(22795): java.lang.NullPointerException
04-25 13:58:44.824: W/System.err(22795): at com.koeriers.standaard.SharedPreferences.setData(SharedPreferences.java:27)
04-25 13:58:44.829: W/System.err(22795): at com.koeriers.standaard.actLogin.onCreate(actLogin.java:79)
04-25 13:58:44.834: W/System.err(22795): at android.app.Activity.performCreate(Activity.java:5206)
04-25 13:58:44.839: W/System.err(22795): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-25 13:58:44.844: W/System.err(22795): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
04-25 13:58:44.849: W/System.err(22795): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
04-25 13:58:44.854: W/System.err(22795): at android.app.ActivityThread.access$700(ActivityThread.java:140)
04-25 13:58:44.859: W/System.err(22795): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
04-25 13:58:44.864: W/System.err(22795): at android.os.Handler.dispatchMessage(Handler.java:99)
04-25 13:58:44.869: W/System.err(22795): at android.os.Looper.loop(Looper.java:137)
04-25 13:58:44.874: W/System.err(22795): at android.app.ActivityThread.main(ActivityThread.java:4921)
04-25 13:58:44.874: W/System.err(22795): at java.lang.reflect.Method.invokeNative(Native Method)
04-25 13:58:44.879: W/System.err(22795): at java.lang.reflect.Method.invoke(Method.java:511)
04-25 13:58:44.884: W/System.err(22795): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
04-25 13:58:44.889: W/System.err(22795): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
04-25 13:58:44.894: W/System.err(22795): at dalvik.system.NativeStart.main(Native Method)
这是我的 SharedPreferences 类:
package com.myapp.standaard;
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.widget.EditText;
public class SharedPreferences extends PreferenceActivity {
public static String GUID = "8BB4894B-92F7-45AF-9A40-B99D7A06A506";
public static String LicenseKey = "letmein";
EditText etLicense;
android.content.SharedPreferences sharedPrefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
addPreferencesFromResource(R.xml.preferences);
etLicense = (EditText) findViewById(R.id.etLicense);
} catch (Exception e) {
e.printStackTrace();
}
}
public Boolean setData(String setPreference, String setValue) {
android.content.SharedPreferences.Editor editor = sharedPrefs.edit();
editor.putString(setPreference, setValue);
editor.commit();
return true;
}
public String getData(String getPreference, String errorMessage) {
sharedPrefs = getSharedPreferences(getPreference, MODE_PRIVATE);
String returnData = sharedPrefs.getString(getPreference, errorMessage);
return returnData;
}
}
这是我的活动中我试图从我的偏好中检索值的地方:
try{
SharedPreferences sp = new SharedPreferences();
licentie = sp.getData("LicenseKey", "Error loading license");
} catch (Exception e){
e.printStackTrace();
licentie = "";
}