我正在使用带有片段的TabLayout活动。一切正常,直到另一个活动出现在前面,当它返回到TabLayout活动时!它有时会抛出一个nullpointexception。
此功能在我的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();
}
}
但是,我在首选项对象上获得了一个nullpointer异常,而这是在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)
有时它可以工作,但有时会抛出一个nullpointer异常。我该如何解决这个问题?
答案 0 :(得分:0)
问题是您的代码可能在创建视图之前执行。
注意您是如何在OnCreateView
中初始化此值的。根据设备当前执行代码的速度,调用片段方法的代码可能在调用片段中的OnCreateView
之前执行,这是您的nullpointer的来源。
我发现您需要根据设置调用主活动中的方法。将此代码移至您的片段OnCreateView
,并将您需要在MainActivity中调用的方法公开。
然后,像这样在片段中访问它们
((MainActivity)getActivity()).stopDisco()