java.lang.IllegalArgumentException: The observer is null.异常解决方案

本文详细介绍了在使用Android 4.0系统时,遇到LinearLayout的removeAll方法出现IllegalArgumentException错误的问题,并提供了解决方案。通过避免在view离开窗口时调用removeView方法,而是将view的引用缓存起来并设置为gone状态,实现了问题的解决。同时,文章还提到了一种替代方法和另一种可能的解决方案。

今天在调用LinearLayout的removeAll方法时出现一个奇怪的bug,

 java.lang.IllegalArgumentException: The observer is null.

android2.3正常,而android4.0出现。

后来经过研究发现是因为4.0系统android.widget.AbsListView.onDetachedFromWindow第一次调用的时候会把observer设置为null,

onDetachedFromWindow方法的作用是当view离开窗口时调用,而我在每次view离开窗口并没有销毁,因为后面要用到,所以

当第二次重用view的时候在remove view时就会再次的调用onDetachedFromWindow,所以就会出现这样的错误,具体可以查看

4.0和2.3的源码onDetachedFromWindow方法源码。

1.因为我是做一个类似5个底部导航键的东西,所以我是这么解决的,不调用removeview了,直接把view的引用缓存起来,然后设置为gone.再想

展示的时候直接设置成visiable。

代码是

点击选项卡后 传一个参数  t  为要启动的界面号

 View currentView = getLocalActivityManager().startActivity(t + "", intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))
    .getDecorView();
   //展示控制:遍历list中的view,与当前view不同则设置为不显示,否则显示
   for(int i =0;i<viewList.size();i++){
    if(null!=viewList.get(i)&&!viewList.get(i).equals(currentView)){
     viewList.get(i).setVisibility(View.GONE);
    }else if(null!=viewList.get(i)&&viewList.get(i).equals(currentView)){
     viewList.get(i).setVisibility(View.VISIBLE);
    }
   }
//   mActivityContainer.removeAllViews();
   //存储控制:list中存储的对应位置的view和当前view不同时进行存储
   if(null==viewList.get(t)||!viewList.get(t).equals(currentView)){
    mActivityContainer.addView(currentView);
    viewList.add(t,currentView);
   }

曲线救国吧 嘿嘿。

2.也参考了别人的代码

如果你的viewgroup有adapter,那就简单了,在adapter中重写
@Override
public void unregisterDataSetObserver(DataSetObserver observer) {
     if (observer != null) {
         super.unregisterDataSetObserver(observer);
     }
 }

直接就可以了

3.或者我猜测还有一种解决方法,在每次view离开窗口时销毁。
大概是view.destroyDrawingCache(),或者别的什么销毁方法。
没试验不知道效果如何仅供参考。
  

 

 

这个异常 `java.lang.IllegalArgumentException: The view is not a child of CoordinatorLayout` 通常发生在你尝试使用 `BottomSheetBehavior.from(view)` 时,但传入的 `view` 并不是 `CoordinatorLayout` 的直接子视图。 ### 常见原因和解决方法: #### ✅ 原因: `BottomSheetBehavior` 只能作用于 `CoordinatorLayout` 的**直接子 View**。如果你尝试通过 `BottomSheetBehavior.from(findViewById(...))` 获取一个不在 `CoordinatorLayout` 中的视图,就会抛出此异常。 #### ✅ 解决方法: 1. **确保使用的是 `BottomSheetDialogFragment` 的正确结构** `BottomSheetDialogFragment` 默认使用的是 `com.google.android.material.R.id.design_bottom_sheet`,必须确保你获取的是这个 ID,并且它在 `CoordinatorLayout` 下。 ```java @Override public void onStart() { super.onStart(); BottomSheetDialog dialog = (BottomSheetDialog) getDialog(); if (dialog != null) { View bottomSheet = dialog.findViewById(com.google.android.material.R.id.design_bottom_sheet); if (bottomSheet != null) { BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet); behavior.setPeekHeight(400); } } } ``` 2. **不要对非 BottomSheet 的 View 使用 `BottomSheetBehavior.from()`** 确保你传入的 View 是 `design_bottom_sheet` 或者你自定义的 `BottomSheet` 布局,并且是 `CoordinatorLayout` 的子 View。 3. **如果你自定义了布局**,请确保结构如下: ```xml <androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <View android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="300dp" android:background="@android:color/white" android:layout_gravity="bottom" android:elevation="8dp" android:fitsSystemWindows="true" app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior" /> </androidx.coordinatorlayout.widget.CoordinatorLayout> ``` 4. **在 DialogFragment 中使用时注意时机** 一定要在 `onStart()` 或 `onActivityCreated()` 生命周期中获取 `BottomSheetBehavior`,不能在 `onCreateView()` 中获取,因为此时 Dialog 的内容还未绑定。 ---
评论 3
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值