What is triggering this Exception instance: “java.lang.IllegalArgumentException: The observer is null.” and how could it be avoid?

前端 未结 2 1110
感动是毒
感动是毒 2020-12-28 22:25

I\'m getting this exception when returning to the original ListActivity after opening an new activity with the content of the item selected by the user. It only

相关标签:
2条回答
  • 2020-12-28 22:58

    I made the silly mistake of thinking that none of my classes were mentioned in the trace, but LoadingDataView is one of them. It doesn't show in the original trace but another that was related.

    Inside that class there is an anonymous ArrayAdapter that was where the incident is happening so I added this as a work around:

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
        if (observer != null) {
            super.unregisterDataSetObserver(observer);
        }
    }
    

    And it seems to work now although I'm still not sure why this method was called twice.

    Although, for now on I'm going to use Fragments as much as I can ;)

    0 讨论(0)
  • 2020-12-28 23:00

    This problem was introduced in Android 4.0.3, and the Observable class was changed to throw an exception when an observer was released more than once. It has been reported as a bug and can be read here: http://code.google.com/p/android/issues/detail?id=22946.

    The easiest way to work around this problem is to wrap the underlying adapter and avoid multiple releases.

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {
      if (observer != null) {
        super.unregisterDataSetObserver(observer);
      }
    } 
    

    But that will not work in all cases, e.g. ExpandableListView has an internal adapter that cannot be accessed. An alternative solution here is to wrap the ExpandableListView and catch the exception. This solution worked for me, and I have yet not found any side effects.

    public class PatchedExpandableListView extends ExpandableListView {
    
      public PatchedExpandableListView(Context context, AttributeSet attrs) {
        super(context, attrs);
      }
    
      @Override
      protected void onDetachedFromWindow() {
        try {
          super.onDetachedFromWindow();
        } catch(IllegalArgumentException iae) {
          // Workaround for http://code.google.com/p/android/issues/detail?id=22751
        }
      }
    }
    
    0 讨论(0)
提交回复
热议问题