java.lang.IllegalArgumentException: Receiver not registered
错误提示:java.lang.IllegalArgumentException: Receiver not registered。。
顾名思义就是Receiver没有被注册,这是由于取消了没有注册的Receiver导致的错误。
我们使用Receiver时必须保证注册(register)和反注册(unregister)成对出现。
遇到这种问题时,你需要检查下自己代码中注册和反注册的代码,看下是否存在多次调用unregister的时候。
很多时候都是想当然认为会那样,但是程序中可能有我们未预料的出口,比如我的程序中在onCreate注册,finish取消注册,理论进入、退出时应该 成对出现,但实际报错了!分析到最后是原布局中使用了TabActivity、GroupActivity等多种结构嵌套,退出当前Activity 时,finish方法被执行了多次。
所以建议Receiver注册和反注册的过程放到onResume和onPause过程中,两个理由:
(1)这两个过程肯定会成对出现,因此可以避免上述错误。
(2)一般我们使用Receiver是为了接受通知来更改UI(特殊情况除外),而对于Activity(UI)界面位于后台时,没必要去接受通知更改UI。
示例:
在 Adroid2.1 和 Adroid2.2 设备上,如果 Activity 中使用到 ViewFlipper 控件,进行横竖屏切换操作时会发生如下错误信息
java.lang.IllegalArgumentException: Receiver not registered: android.widget.ViewFlipper$1@43dee3c0
这是由于 onDetachedFromWindow() 莫名其妙地在 onAttachedToWindow() 之前被调用了。据说是一个 Bug ,不知在最新的 2.3 上是否修正。
下面提供一个很简单的解决方案,重写 ViewFlipper 的 onDetachedFromWindow() 方法
@Override
protected void onDetachedFromWindow () {
try {
super.onDetachedFromWindow();
}
catch (IllegalArgumentException e) {
stopFlipping();
}
}
方法二: 这种方法是放在onresume里面初始化注册 在onpause里面取消注册
@Override protected void onResume() { super.onResume(); if (receiver == null) { filter = new IntentFilter(ResponseReceiver.ACTION_RESP); filter.addCategory(Intent.CATEGORY_DEFAULT); receiver = new ResponseReceiver(); registerReceiver(receiver, filter); } } @Override protected void onPause() { super.onPause(); if (receiver != null) { unregisterReceiver(receiver); receiver = null; } }
方法三:在ondestory里面使其receiver为空
@Override
if (batteryNotifyReceiver!=null) {
unregisterReceiver(batteryNotifyReceiver);
batteryNotifyReceiver=null;
}
[/code]
- Android广播接收器注册问题:Caused by: java.lang.IllegalArgumentException: Receiver not registered
- [Android]Caused by: java.lang.IllegalArgumentException: Service not registered.md
- Mybatis报错:java.lang.IllegalArgumentException: Result Maps collection does not contain value for java
- mybatis报错:java.lang.IllegalArgumentException: Mapped Statements collection does not contain
- java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for queryAll
- java.lang.illegalargumentexception view android.widget.listview is not a sliding drawer
- java.lang.IllegalArgumentException: column '_id' does not exist问题的解决方案
- Caused by: java.lang.IllegalArgumentException: Original must not be null
- java. lang. IllegalArgumentException: Path does not start with a "/" character
- java.lang.IllegalArgumentException: not found @HttpResponse from class java.lang.Object解决方法
- java.lang.illegalargumentexception view not attached to window manager 解决
- java.lang.IllegalArgumentException: Can not create a Path from a null string
- 使用Activity.isfinishing()解决java.lang.IllegalArgumentException: View not attached to window manager
- Caused by: java.lang.IllegalArgumentException: Not an managed type: class
- Java.lang.IllegalArgumentException: prefix wsdp is not bound to a namespace。
- java.lang.IllegalArgumentException: You must not call setTag() on a view Glide is targeting
- java.lang.IllegalArgumentException: View not attached to window manager
- struts错误记录(java.lang.IllegalArgumentException: Path userLogin.jsp does not star)
- Caused by: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value