我是Java的新手。我正在制作一个使用SharedPreferences的应用程序。我使用的具体教程是这样的:
http://blog.donnfelker.com/2011/02/17/android-a-simple-eula-for-your-android-apps/
我稍微修改了一下代码,但它给了我一个错误:
public class SimpleEula extends Application {
private String EULA_PREFIX = "eula";
public Activity mActivity;
public SimpleEula(Activity context) {
mActivity = context;
}
public void showEula() {
SharedPreferences sh_showEula;
final String eulaKey = EULA_PREFIX;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
...
我在每行之后记录我的代码,我的应用程序崩溃在:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
即使我将行更改为:
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
我仍然遇到了崩溃。我不知道问题是什么。有人能帮助我吗?
提前致谢
修改
这些是LogCat中的错误:
08-21 20:44:58.905: E/AndroidRuntime(610): FATAL EXCEPTION: main
08-21 20:44:58.905: E/AndroidRuntime(610): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.MainActivity}: java.lang.NullPointerException
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.ActivityThread.access$600(ActivityThread.java:123)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.os.Handler.dispatchMessage(Handler.java:99)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.os.Looper.loop(Looper.java:137)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.ActivityThread.main(ActivityThread.java:4424)
08-21 20:44:58.905: E/AndroidRuntime(610): at java.lang.reflect.Method.invokeNative(Native Method)
08-21 20:44:58.905: E/AndroidRuntime(610): at java.lang.reflect.Method.invoke(Method.java:511)
08-21 20:44:58.905: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
08-21 20:44:58.905: E/AndroidRuntime(610): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
08-21 20:44:58.905: E/AndroidRuntime(610): at dalvik.system.NativeStart.main(Native Method)
08-21 20:44:58.905: E/AndroidRuntime(610): Caused by: java.lang.NullPointerException
08-21 20:44:58.905: E/AndroidRuntime(610): at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:371)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:366)
08-21 20:44:58.905: E/AndroidRuntime(610): at com.test.test.SimpleEula.show(SimpleEula.java:23)
08-21 20:44:58.905: E/AndroidRuntime(610): at com.test.test.MainActivity.onCreate(MainActivity.java:26)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.Activity.performCreate(Activity.java:4465)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
08-21 20:44:58.905: E/AndroidRuntime(610): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
08-21 20:44:58.905: E/AndroidRuntime(610): ... 11 more
答案 0 :(得分:0)
最好尝试将getApplicationContext()
用于Application类,
SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(getApplicationContext());
我做什么,
public class SimpleEula extends Application {
private Context mContext;
@Override
public void onCreate() {
super.onCreate();
mContext = this; // use this mContext
}
public void showEula() {
SharedPreferences prefs = PreferenceManager.
getDefaultSharedPreferences(mContext);
}
}
答案 1 :(得分:0)
NullPointerExepter是通过扩展Activity或Application类引起的。此代码有效:
public class SimpleEula {
private String EULA_PREFIX = "eula";
private Activity mActivity;
public SimpleEula(Activity context) {
mActivity = context;
}
public void show() {
final String eulaKey = EULA_PREFIX;
final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mActivity);
...
现在它有效。谢谢大家的帮助