我是 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