我在我的主要活动中使用BroadcastReceiver并将其注册到OnCreate并尝试将其注册到OnDestroy中,但一段时间应用程序崩溃,因为它声明我正试图取消注册已注销的接收器。这里是我的代码:Unregister Receiver无法取消注册onDestroy
@Override
public void onCreate(Bundle savedInstanceState) {
...
LocalBroadcastManager.getInstance(this).registerReceiver(forceLogoutRequest, new IntentFilter(CommonUtils.FORCE_LOGOUT));
}
@Override
public void onDestroy() {
super.onDestroy();
if(forceLogoutRequest!=null) {
unregisterReceiver(forceLogoutRequest);
forceLogoutRequest = null;
}
}
private BroadcastReceiver forceLogoutRequest = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
..
..
}
};
这是我收到的控制台:
10-12 13:28:34.644 5772-6344/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.nurse, PID: 5772
java.lang.RuntimeException: Unable to destroy activity java.lang.IllegalArgumentException: Receiver not registered: [email protected]
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3812)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3830)
at android.app.ActivityThread.access$1400(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
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:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.IllegalArgumentException: Receiver not registered: [email protected]
at android.app.LoadedApk.forgetReceiverDispatcher(LoadedApk.java:771)
at android.app.ContextImpl.unregisterReceiver(ContextImpl.java:1671)
at android.content.ContextWrapper.unregisterReceiver(ContextWrapper.java:511)
at com.myapp.hp.nurse.activity.MainActivity.onDestroy(MainActivity.java:327)
at com.myapp.hp.nurse.activity.NfcEnabledActivity.onDestroy(NfcEnabledActivity.java:49)
at android.app.Activity.performDestroy(Activity.java:6169)
at android.app.Instrumentation.callActivityOnDestroy(Instrumentation.java:1141)
at android.app.ActivityThread.performDestroyActivity(ActivityThread.java:3799)
at android.app.ActivityThread.handleDestroyActivity(ActivityThread.java:3830)
at android.app.ActivityThread.access$1400(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1405)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5373)
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:1020)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
10-12 13:28:36.316 5772-6344/? I/Process: Sending signal. PID: 5772 SIG: 9
这可能是接收器被打死你试图杀死它 – Zoe
检查这个https://stackoverflow.com/questions前/ 6165070/receiver-not-registered-exception-error/24391706#24391706 – tamtom
可能重复[Receiver not registered exception error?](https://stackoverflow.com/questions/6165070/receiver-not-registered-exception-error ) –