我正在尝试使用SharedPreference
存储一些值并稍后访问它们,但不知道为什么我的Activity
崩溃并给我这个错误。
06-12 11:03:40.459 1525-1525/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.ets.medecord/com.ets.medecord.SplashActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1680)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getPackageName(ContextWrapper.java:120)
at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353)
at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348)
at com.ets.medecord.SplashActivity.<init>(SplashActivity.java:15)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1409)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1672)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
at android.app.ActivityThread.access$1500(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3835)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
at dalvik.system.NativeStart.main(Native Method)
我在SplashActivity.java
中使用的代码。
当我运行我的应用程序时它会崩溃,但如果我删除包含SharedPreference
的那些行。
public class SplashActivity extends ActionBarActivity{
static final int PAUSE_TIME = 2000;
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
boolean isAppFirstStart = pref.getBoolean("start",true);
Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
handler = new Handler();
getSupportActionBar().hide();
new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(PAUSE_TIME);
} catch(InterruptedException e) {
e.printStackTrace();
}
handler.post(new Runnable() {
@Override
public void run() {
if(isAppFirstStart) {
editor.putBoolean("start",false);
editor.commit();
startActivity(new Intent(getApplicationContext(), IntroActivity.class));
finish();
} else {
startActivity(new Intent(getApplicationContext(), LoginActivity.class));
finish();
}
}
});
}
}).start();
}
}
答案 0 :(得分:2)
外部课程方法和static initialization blocks,this
为null
。它尚未初始化。
因此,您无法在这些地方传递this
。
答案 1 :(得分:1)
问题在于:
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
将您的共享偏好行移至onCreate,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = pref.edit();
...
}
然后它会起作用。
PreferenceManager.getDefaultSharedPreferences(this)
给出空指针异常的原因是因为this
之前无法解析onCreate()
。