Receiver not registered exception error?

后端 未结 10 2241
醉酒成梦
醉酒成梦 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:09

    As mentioned in other answers, the exception is being thrown because each call to registerReceiver is not being matched by exactly one call to unregisterReceiver. Why not?

    An Activity does not always have a matching onDestroy call for every onCreate call. If the system runs out of memory, your app is evicted without calling onDestroy.

    The correct place to put a registerReceiver call is in the onResume call, and unregisterReceiver in onPause. This pair of calls is always matched. See the Activity lifecycle diagram for more details. http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle

    Your code would change to:

    SharedPreferences mPref
    IntentFilter mFilter;
    
    @Override
    public void onCreate(){
        super.onCreate();
        mPref = PreferenceManager.getDefaultSharedPreferences(this);
        mFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
        filter.addAction(Intent.ACTION_POWER_CONNECTED);
        filter.addAction(Intent.ACTION_POWER_DISCONNECTED);
     }
    
    @Override
    public void onResume() {
        registerReceiver(batteryNotifyReceiver,mFilter);
        mPref.registerOnSharedPreferenceChangeListener(this);
    }
    
    @Override
    public void onPause(){
         unregisterReceiver(batteryNotifyReceiver, mFilter);
         mPref.unregisterOnSharedPreferenceChangeListener(this);
    }
    
    0 讨论(0)
  • 2020-11-28 06:09

    Declare receiver as null and then Put register and unregister methods in onResume() and onPause() of the activity respectively.

    @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;
        }
    }
    
    0 讨论(0)
  • 2020-11-28 06:12

    Use this code everywhere for unregisterReceiver:

    if (batteryNotifyReceiver != null) {
        unregisterReceiver(batteryNotifyReceiver);
        batteryNotifyReceiver = null;
    }
    
    0 讨论(0)
  • 2020-11-28 06:13

    I used a try - catch block to solve the issue temporarily.

    // Unregister Observer - Stop monitoring the underlying data source.
            if (mDataSetChangeObserver != null) {
                // Sometimes the Fragment onDestroy() unregisters the observer before calling below code
                // See <a>http://stackoverflow.com/questions/6165070/receiver-not-registered-exception-error</a>
                try  {
                    getContext().unregisterReceiver(mDataSetChangeObserver);
                    mDataSetChangeObserver = null;
                }
                catch (IllegalArgumentException e) {
                    // Check wether we are in debug mode
                    if (BuildConfig.IS_DEBUG_MODE) {
                        e.printStackTrace();
                    }
                }
            }
    
    0 讨论(0)
  • 2020-11-28 06:14

    For anybody who will come upon this problem and they tried all that was suggested and nothing still works, this is how I sorted my problem, instead of doing LocalBroadcastManager.getInstance(this).registerReceiver(...) I first created a local variable of type LocalBroadcastManager,

    private LocalBroadcastManager lbman;
    

    And used this variable to carry out the registering and unregistering on the broadcastreceiver, that is

    lbman.registerReceiver(bReceiver);
    

    and

    lbman.unregisterReceiver(bReceiver);
    
    0 讨论(0)
  • 2020-11-28 06:16

    EDIT: This is the answer for inazaruk and electrichead... I had run into a similar issue to them and found out the following...

    There is a long-standing bug for this problem here: http://code.google.com/p/android/issues/detail?id=6191

    Looks like it started around Android 2.1 and has been present in all of the Android 2.x releases since. I'm not sure if it is still a problem in Android 3.x or 4.x though.

    Anyway, this StackOverflow post explains how to workaround the problem correctly (it doesn't look relevant by the URL but I promise it is)

    Why does keyboard-slide crash my app?

    0 讨论(0)
提交回复
热议问题