Receiver not registered exception error?

后端 未结 10 2242
醉酒成梦
醉酒成梦 2020-11-28 05:41

In my developer console people keep reporting an error that I cannot reproduce on any phone I have. One person left a message saying he gets it when they try to open the set

相关标签:
10条回答
  • 2020-11-28 06:17

    The root of your problem is located here:

     unregisterReceiver(batteryNotifyReceiver);
    

    If the receiver was already unregistered (probably in the code that you didn't include in this post) or was not registered, then call to unregisterReceiver throws IllegalArgumentException. In your case you need to just put special try/catch for this exception and ignore it (assuming you can't or don't want to control number of times you call unregisterReceiver on the same recevier).

    0 讨论(0)
  • 2020-11-28 06:19

    When the UI component that registers the BR is destroyed, so is the BR. Therefore when the code gets to unregistering, the BR may have already been destroyed.

    0 讨论(0)
  • 2020-11-28 06:25

    Be careful, when you register by

    LocalBroadcastManager.getInstance(this).registerReceiver()
    

    you can't unregister by

     unregisterReceiver()
    

    you must use

    LocalBroadcastManager.getInstance(this).unregisterReceiver()
    

    or app will crash, log as follow:

    09-30 14:00:55.458 19064-19064/com.jialan.guangdian.view E/AndroidRuntime: FATAL EXCEPTION: main Process: com.jialan.guangdian.view, PID: 19064 java.lang.RuntimeException: Unable to stop service com.google.android.exoplayer.demo.player.PlayService@141ba331: java.lang.IllegalArgumentException: Receiver not registered: com.google.android.exoplayer.demo.player.PlayService$PlayStatusReceiver@19538584 at android.app.ActivityThread.handleStopService(ActivityThread.java:2941) at android.app.ActivityThread.access$2200(ActivityThread.java:148) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1395) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5310) at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) Caused by: java.lang.IllegalArgumentException: Receiver not registered: com.google.android.exoplayer.demo.player.PlayService$PlayStatusReceiver@19538584 at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:769) at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1794) at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:510) at com.google.android.exoplayer.demo.player.PlayService.onDestroy(PlayService.java:542) at android.app.ActivityThread.handleStopService(ActivityThread.java:2924) at android.app.ActivityThread.access$2200(ActivityThread.java:148)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1395)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:135)  at android.app.ActivityThread.main(ActivityThread.java:5310)  at java.lang.reflect.Method.invoke(Native Method)  at java.lang.reflect.Method.invoke(Method.java:372)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:901)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 

    0 讨论(0)
  • 2020-11-28 06:26

    Lets assume your broadcastReceiver is defined like this:

    private BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
    
            // your code
    
        }
    };
    

    If you are using LocalBroadcast in an Activity, then this is how you'll unregister:

    LocalBroadcastManager.getInstance(this).unregisterReceiver(broadcastReceiver);
    

    If you are using LocalBroadcast in a Fragment, then this is how you'll unregister:

    LocalBroadcastManager.getInstance(getActivity()).unregisterReceiver(broadcastReceiver);
    

    If you are using normal broadcast in an Activity, then this is how you'll unregister:

    unregisterReceiver(broadcastReceiver);
    

    If you are using normal broadcast in a Fragment, then this is how you'll unregister:

    getActivity().unregisterReceiver(broadcastReceiver);
    
    0 讨论(0)
提交回复
热议问题