简体   繁体   English

NullPointerException,同时恢复活动

[英]NullPointerException while resuming activity

I am getting this crash on a Nexus device (a Sony one with the same OS version does not crash): 我在Nexus设备上遇到此崩溃(具有相同操作系统版本的Sony不会崩溃):

02-20 14:38:14.551 22255 22255 I DebugActivity: java.lang.NullPointerException: Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.support.v4.app.FragmentManagerImpl.ensureExecReady(FragmentManager.java:2180)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2233)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:379)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.support.v4.app.FragmentActivity.onResume(FragmentActivity.java:461)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at com.whatever.sdk.MyActivity.onResume(MyActivity.java:109)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at com.whatever.sdk.MyActivity$1.onCallStateChanged(MyActivity.java:231)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.telephony.PhoneStateListener$2.handleMessage(PhoneStateListener.java:295)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.os.Looper.loop(Looper.java:148)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at android.app.ActivityThread.main(ActivityThread.java:5417)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at java.lang.reflect.Method.invoke(Native Method)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
02-20 14:38:14.551 22255 22255 I DebugActivity:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

And here is the relevant piece of code: 这是相关的代码段:

public class MyActivity extends AppCompatActivity {
...
    private final PhoneStateListener phoneStateListener = new PhoneStateListener() {
        @Override
        public void onCallStateChanged(int state, String incomingNumber) {
            if(state == TelephonyManager.CALL_STATE_IDLE) {
                MyActivity.this.onResume();
...
    @Override
    protected void onResume() {
        try {
            super.onResume();
...

The activity is not declared with any launchMode and trying to add different flags such as Intent.FLAG_ACTIVITY_CLEAR_TOP when starting the activity did not help. 该活动未使用任何launchMode声明,并且在启动活动没有帮助时尝试添加其他标志,例如Intent.FLAG_ACTIVITY_CLEAR_TOP We have supportLibraryVersion = '27.0.2' 我们有supportLibraryVersion = '27.0.2'

Is there an obvious fix to this and why is it happening on some devices and not on others? 有明显的解决方法,为什么它在某些设备上而不在其他设备上发生?

Instead of calling Android lifecycle method like onResume() in onCallStateChanged() just remove the relevant code of onResume() in a separate method and call that method from onCallStateChanged() . 除了在onCallStateChanged()中调用诸如onResume()类的Android生命周期方法外,只需在单独的方法中删除onResume()的相关代码,然后从onCallStateChanged()调用该方法onCallStateChanged() That way when the Activity will resume the relevant code gets called and also when needed you can call that method to run the relevant code. 这样,当Activity将恢复相关代码时,就会被调用,并且在需要时也可以调用该方法来运行相关代码。 Ofcourse you will need to add appropriate object condition checking in this newly created method. 当然,您将需要在此新创建的方法中添加适当的对象条件检查。

From the error log I can see you are working with Fragments. 从错误日志中,我可以看到您正在使用Fragments。 I have encountered similar error before. 我之前遇到过类似的错误。 One other thing comes to mind is saving a reference to your MainActivity eg in Fragments in a reference like Activity mainActivityRef = getActivity() . 我想到的另一件事是Activity mainActivityRef = getActivity() MainActivity的引用保存在Fragment中的类似Activity mainActivityRef = getActivity()的引用中。 Later try to use this as mainActivityRef.onMyFunc() . 稍后尝试将其用作mainActivityRef.onMyFunc() This has saved me from this error Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference 这使我免于此错误Attempt to invoke virtual method 'android.os.Handler android.support.v4.app.FragmentHostCallback.getHandler()' on a null object reference

Credit to @ben-p's comment for the undocumented info on "only the Android framework should call [onResume()]" (mind you, this is a public method, along with onPostResume ), the solution was to remove phoneStateListener . 感谢@ ben-p对“仅Android框架应调用[onResume()]”上未记录信息的评论(请注意,这是一个公共方法,以及onPostResume ),解决方案是删除phoneStateListener Testing on the emulator, it is clear that the activity (and the video it hosts) is correctly paused and resumed by the framework when an incoming call notification is received and when the notification is dismissed or when the user finishes the phone call. 在模拟器上进行测试后,很明显,当收到来电通知,取消通知或用户结束电话通话时,框架会正确暂停并恢复活动(及其托管的视频)。 The confusing part is that on most devices, calling onResume() directly does not lead to a crash, hiding the bug. 令人困惑的是,在大多数设备上,直接调用onResume() 不会导致崩溃,从而隐藏了错误。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM