为什么我的getPreferences方法返回null?

时间:2014-09-03 15:18:55

标签: android

这是我打算用来访问SharedPreferences的类,但是当我运行app时,我得到一个NullPointerException。

public class UserPreferences extends PreferenceActivity{

private SharedPreferences prefs;

public UserPreferences(){
    prefs = getPreferences(Context.MODE_PRIVATE);
}
public String getBackupDirectoryPath(){
    return prefs.getString("backupPath", Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath());
}
public void setBackupDirectoryPath(String path){
    Editor editor = prefs.edit();
    editor.putString("backupPath", path);
    editor.commit();
}

}

这是堆栈跟踪。主要问题是在这个类的第14行,这意味着:prefs = getPreferences(Context.MODE_PRIVATE);

09-03 18:17:14.733: E/AndroidRuntime(1081): FATAL EXCEPTION: main
09-03 18:17:14.733: E/AndroidRuntime(1081): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.linicdecor/com.example.linicdecor.Settings}: java.lang.NullPointerException
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.ActivityThread.access$700(ActivityThread.java:154)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.os.Handler.dispatchMessage(Handler.java:99)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.os.Looper.loop(Looper.java:137)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.ActivityThread.main(ActivityThread.java:5306)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at java.lang.reflect.Method.invokeNative(Native Method)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at java.lang.reflect.Method.invoke(Method.java:511)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at dalvik.system.NativeStart.main(Native Method)
09-03 18:17:14.733: E/AndroidRuntime(1081): Caused by: java.lang.NullPointerException
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.Activity.getLocalClassName(Activity.java:4557)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.Activity.getPreferences(Activity.java:4590)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at com.example.linicdecor.UserPreferences.<init>(UserPreferences.java:14)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at com.example.linicdecor.MyFile.<init>(MyFile.java:21)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at com.example.linicdecor.Settings.initialize(Settings.java:30)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at com.example.linicdecor.Settings.onCreate(Settings.java:22)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.Activity.performCreate(Activity.java:5255)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
09-03 18:17:14.733: E/AndroidRuntime(1081):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)

0 个答案:

没有答案