Possible Duplicate:
Receiver not registered exception error?
I have a TabActivity and the 'content' of each TabSpec is my own GuideListActivity class.
There are seven tabs (one for each day of the week) and each GuideListActivity shows TV Guide info for one TV channel / one day of the week.
The TabActivity maintains which channel number the user is viewing guide details for and when the user changes to view another channel's info, the TabActivity uses sendStickyBroadcast() with an Intent identifying which channel's info to display (having first removed any previous sticky broadcast that may exist).
This works but I'm getting seemingly random issues with the GuideListActivity's BroadcastReceiver which are too frequent to ignore (ev开发者_开发问答en in 'Beta' level code). The error I see is...
java.lang.RuntimeException: Unable to pause activity {com.mycompany.mypackage/com.mycompany.mypackage.GuideListActivity}: java.lang.IllegalArgumentException: Receiver not registered: com.mycompany.mypackage.GuideListActivity$ChannelChangeListener@462ebe20
Each GuideListActivity maintains its own BroadcastReceiver (obviously) example...
public class GuideListActivity extends ListActivity {
private ChannelChangeListener listener = null;
private Intent ChannelChangeListenerIntent = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
listener = new ChannelChangeListener();
}
@Override
protected void onResume(){
super.onResume();
if (ChannelChangeListenerIntent == null)
ChannelChangeListenerIntent = registerReceiver(listener, new IntentFilter(packageName + ".GUIDE_UPDATE"));
}
@Override
protected void onPause() {
super.onPause();
if (ChannelChangeListenerIntent != null)
unregisterReceiver(listener); // <== EXCEPTION THROWN HERE
}
// Nested 'listener' class
public class ChannelChangeListener extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
// Handle intent
}
}
}
As I said this seems to be random - I can stick to one channel and click 1, 2 maybe 3 or even all 7 tabs then next click , the exception is thrown. Other times it happens on the second click.
Any help to explain this and how I could fix it would be really appreciated.
Set ChannelChangeListenerIntent
to null in onPause
:
if (ChannelChangeListenerIntent != null) {
unregisterReceiver(listener);
ChannelChangeListenerIntent = null;
}
An activity can be resumed after pausing, so the activity may not register the listener in the second call to onResume
.
Go into your manifest and register the broadcast receiver. And along with this insure that you are allowed to receive that "specific" broadcast in your permissions list.
Also another thing to check is to ensure that your logic of enabling and disabling the receiver (register/unregister) are flowing correctly.
精彩评论