java.lang.IllegalArgumentException:观察者为null

时间:2011-09-03 02:49:15

标签: java android android-viewpager

我的某个应用的用户报告了此错误。我很困惑如何修复它意味着什么。经过一番谷歌搜索......仍然没有运气。有人以前见过这个或知道如何修复它吗?

java.lang.IllegalArgumentException: The observer is null.
at android.database.Observable.unregisterObserver(Observable.java:59)
at android.widget.BaseAdapter.unregisterDataSetObserver(BaseAdapter.java:42)
at android.widget.HeaderViewListAdapter.unregisterDataSetObserver(HeaderViewListAdapter.java:256)
at android.widget.AbsListView.onDetachedFromWindow(AbsListView.java:2309)
at android.view.View.dispatchDetachedFromWindow(View.java:8197)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1968)
at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:1966)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3257)
at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3237)
at android.view.ViewGroup.removeView(ViewGroup.java:3185)
at com.aheudev.a.slickdeals.adapters.DealPageAdapter.destroyItem(DealPageAdapter.java:174)
at android.support.v4.view.ViewPager.populate(ViewPager.java:415)
at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:696)
at android.support.v4.view.ViewPager.computeScroll(ViewPager.java:668)
at android.view.ViewGroup.drawChild(ViewGroup.java:2434)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
at android.view.View.draw(View.java:9282)
at android.view.ViewGroup.drawChild(ViewGroup.java:2584)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
at android.view.ViewGroup.drawChild(ViewGroup.java:2582)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
at android.view.ViewGroup.drawChild(ViewGroup.java:2582)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2189)
at android.view.View.draw(View.java:9282)
at android.widget.FrameLayout.draw(FrameLayout.java:419)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:1923)
at android.view.ViewRoot.draw(ViewRoot.java:1695)
at android.view.ViewRoot.performTraversals(ViewRoot.java:1410)
at android.view.ViewRoot.handleMessage(ViewRoot.java:2040)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:132)
at android.app.ActivityThread.main(ActivityThread.java:4123)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:491)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
at dalvik.system.NativeStart.main(Native Method)

它来自我的PagerAdapter中的这个功能:

@Override
public void destroyItem(View collection, int position, Object view) {
    ((ViewPager) collection).removeView((View) view);
}

3 个答案:

答案 0 :(得分:19)

我也遇到了同样的问题,看来你的DealPageAdapter上的unregisterDataSetObserver被调用了两次。为了解决这个问题,我在我的适配器中覆盖了unregisterDataSetObserver

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

希望这有帮助!

答案 1 :(得分:1)

有些人遇到了您的问题HereHere

从查看调用堆栈看起来,您的“视图”注册了一个观察者,它现在正在尝试取消注册。那个观察者已经不在了,即使它是预期的。因此错误。这可能是一个错误,也可能与你的工作方式有关。

尝试查看Android Source,看看是否可以追踪它。

祝你好运。

答案 2 :(得分:0)

另一种解决方案是包装ListView并捕获异常。这个解决方案对我有用,我还没有发现任何副作用。

    public class CustumListView extends ListView {

  public CustumListView(Context context, AttributeSet attrs) {
    super(context, attrs);
  }

  @Override
  protected void onDetachedFromWindow() {
    try {
      super.onDetachedFromWindow();
    } catch(IllegalArgumentException iae) {

    }
  }
}