我正在使用帶片段的TabLayout活動。一切正常,直到另一個活動在前面,當它返回到TabLayout活動時,它有時會拋出一個nullpointexception。返回應用程序時出現NullpointerException
這個函數在我的MainActivity中。 super.onPause之後的代碼似乎在用戶返回活動時被調用。該代碼通知一個片段,該片段稱爲onpause。
@Override
protected void onPause() {
super.onPause();
myPreferences.setBoolPreferences(MyPreferences.MY_PREFS_START, false);
((FragmentInterface) mSectionsPagerAdapter.getItem(tabLayout.getSelectedTabPosition())).onRunPermissionChanged();
flashLight.off();
}
這裏是片段中的代碼。
@Override
public void onRunPermissionChanged() {
if(myPreferences.getBoolPreferences(MyPreferences.MY_PREFS_START, MyPreferences.MY_PREFS_START_DEFAULT)){
disco.start(viewBackground, imageViewDiscoBall);
} else {
stopDisco();
}
}
不過,我得到的喜好空指針異常對象,而這是在OnCreateView初始化。
07-19 21:32:52.984 7829-7829/com.tomhogenkamp.NeonDiscoLight E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.tomhogenkamp.NeonDiscoLight, PID: 7829
java.lang.RuntimeException: Unable to pause activity {com.tomhogenkamp.NeonDiscoLight/com.tomhogenkamp.NeonDiscoLight.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tomhogenkamp.NeonDiscoLight.MyPreferences.getBoolPreferences(java.lang.String, boolean)' on a null object reference
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3660)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3604)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3572)
at android.app.ActivityThread.access$1100(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1410)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean com.tomhogenkamp.NeonDiscoLight.MyPreferences.getBoolPreferences(java.lang.String, boolean)' on a null object reference
at com.tomhogenkamp.NeonDiscoLight.FragmentDisco.onRunPermissionChanged(FragmentDisco.java:104)
at com.tomhogenkamp.NeonDiscoLight.MainActivity.onPause(MainActivity.java:247)
at android.app.Activity.performPause(Activity.java:6374)
at android.app.Instrumentation.callActivityOnPause(Instrumentation.java:1412)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3639)
at android.app.ActivityThread.performPauseActivity(ActivityThread.java:3604)
at android.app.ActivityThread.handlePauseActivity(ActivityThread.java:3572)
at android.app.ActivityThread.access$1100(ActivityThread.java:150)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1410)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:168)
at android.app.ActivityThread.main(ActivityThread.java:5885)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
有時它可以工作,但有時會拋出一個空指針異常。我該如何解決這個問題?
當你初始化myPreferences? –
在OnCreateView方法中。 – tomhogenkamp
我不知道是否有幫助,但在onPause()你設置布爾首選項,但不提交更改嘗試使用Saredpreferences.Editor並提交更改 –