开发中遇到的问题--java.lang.IllegalStateException

在实际开发中经常会遇到java.lang.IllegalStateException的异常。下面是我所遇到的java.lang.IllegalStateException的解决方法:

1.  在APP首页的导航栏,一共有五个栏目,要求一页只显示4个,另外一个在另一页显示,我用ViewPager实现的,将各个栏目LinearLayout包裹起来,然后在代码中动态添加到ViewPager每一个页面的LinearLayout中,然后在添加的过程中报异常java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.

有异常时的代码:

menuList = new ArrayList<LinearLayout>();
        if(photoAlbumIsShow.equals("1")){
            llMenu1.addView(messageBoard,0);
            //家庭相册
            ViewParent vg1 = albumMenu.getParent();
            if(vg != null){
                ViewGroup group = (ViewGroup) vg1;
                group.removeView(albumMenu);
            }
            llMenu1.addView(albumMenu,1);
            llMenu1.addView(nutrientIndex,2);
            llMenu1.addView(basket,3);
            llMenu2.addView(commonProblem,0);
            menuList.add(llMenu1);
            menuList.add(llMenu2);
        }else{
            llMenu1.addView(messageBoard,0);
            llMenu1.addView(nutrientIndex,1);
            llMenu1.addView(basket,2);
            llMenu1.addView(commonProblem,3);
            menuList.add(llMenu1);
            albumMenu.setVisibility(View.GONE);
            llMenu2.setVisibility(View.GONE);
        }

        if (homeMemuAdapter == null) {
            homeMemuAdapter = new HomeMemuAdapter(menuList, mContext, llMenuPoints);
            menuPager.setAdapter(homeMemuAdapter);
        } else {

        }
        menuPager.setOnPageChangeListener(new MenuPagerChangeListner());
        menuPager.setCurrentItem(0);

各个子项向页面中添加时,报java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.解决方法:

在子项添加前,先获取子项的父控件,然后移除父控件中的子项,例如:

//留言板
ViewParent vg = messageBoard.getParent();
if(vg != null){
    ViewGroup group = (ViewGroup) vg;
    group.removeView(messageBoard);
}
 
2.  将Fragment页放到activity容器中时报java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first
原因:mView = inflater.inflate(R.layout.activity_alarm_list,container);



3.  视频在播放或暂停过程中,按返回键退出。报java.lang.IllegalStateException,

原因:当前对客户端的响应已经结束,不能在响应已经结束(或说消亡)后再向客户端(实际上是缓冲区)输出任何内容。

解决方法:在退出的onDestroy()中没有加player = null

protected void onDestroy(){
    super.onDestroy();
    unregisterReceiver(videoReceiver);
    if(player.isPlaying()){
        player.stop();
    }
    player.release();
    player null;
 
}

4.  java.lang.IllegalStateException: Fatal Exception thrown on Scheduler.Worker

网上的解决方法是:加了红字的部分

observable.subscribeOn(Schedulers.computation())
            .observeOn(AndroidSchedulers.mainThread())
            .unsubscribeOn(Schedulers.io())
            .subscribe(new Subscriber<T>() {
                @Override
                public void onCompleted() {
                }
这个问题我忘记是怎么解决的了。以后遇到再加。


5.  有多个library的项目运行时,报java.lang.IllegalStateException: Required view 'rl_infos' with ID 2131361899 for field 'ivManaBack' and method 'backClick' was not found. If this view is optional add '@Nullable' (fields) or '@Optional' (methods) annotation.

原来运行好好的,突然就出现问题了。多次rebuild和clean都不见效。后来删除板子里的安装,再rebuild和clean了一下,安装后正常。也不知道是rebuld的有效还是重装起的作用。


6.  MainActivity.java页面有四个tab页,第一个是首页,其他依次是圈圈,商城,食材管理和冰箱控制页,在首页点击各个栏目可以跳转到其他tab页,在跳转的过程中弹出了一个dialog,导致出现java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState

跟踪异常,是在transaction.commit();报的。在网上查资料得出:

commit方法是在Activity的onSaveInstanceState()之后调用的,这样会出错,因为onSaveInstanceState方法是在该Activity即将被销毁前调用,来保存Activity数据的,如果在保存玩状态后再给它添加Fragment就会出错。解决办法就是把commit()方法替换成 commitAllowingStateLoss()就行了,其效果是一样的。


7.  小米5手机在获取本地视频时,报CursorWindow: Failed to read row 0, column 2 from a CursorWindow which has 1 rows, 2 columns.

java.lang.IllegalStateException: Couldn't read row 0, col 2 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.

原因:

Cursor cursor1 = getContentResolver().query(contentURI, null, null,
        null, null);
在打印cursor1.getString(2)时报的异常。

Log.d("video----->",  "视频绝对路径" + cursor1.getString(2));

经过debug,cursor1的长度为2,没有第3个,

cursor1.getString(0)是获取视频文件名,
cursor1.getString(1)是获取视频文件的ID。

获取小米5本地视频的路径Uricontent://com.android.fileexplorer.fileprovider/external_files/tencent/MicroMsg/WeiXin/wx_camera_1490852824886.mp4只能用

contentURI.getPath()拼接。

 
 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
mybatis-puls报错java.lang.illegalstateexception: no typehandler found fo的原因是没有找到合适的类型处理器。 MyBatis-Puls是MyBatis的一个插件,用于增强MyBatis框架的功能。在使用MyBatis-Puls的过程,如果出现了"no typehandler found fo"的错误,通常是由于没有正确配置类型处理器所致。 类型处理器(TypeHandler)是MyBatis用于将数据库的数据类型与Java的数据类型进行转换的一个重要组件。当MyBatis-Puls在映射对象和数据库表之间进行数据转换时,会根据声明的类型处理器来进行相应的转换操作。 要解决这个错误,我们需要确保在MyBatis-Puls的配置文件正确配置了类型处理器。 首先,确认错误信息提到的类型处理器确实存在于项目。如果没有找到合适的类型处理器,我们可以自定义一个类型处理器,实现TypeHandler接口,并在配置文件进行注册。 其次,确认类型处理器的配置是否正确。在MyBatis-Puls的配置文件,可以通过typeHandlers标签来配置类型处理器。确保需要的类型处理器已经被正确配置。 最后,检查映射对象的属性与数据库字段的类型是否匹配。如果属性的类型与数据库字段的类型不一致,也会导致类型处理器报错。确保属性和字段的类型匹配,或者在MyBatis-Puls的配置文件配置适当的类型处理器来解决类型转换问题。 总之,当出现mybatis-puls报错java.lang.illegalstateexception: no typehandler found fo时,需要检查是否正确配置了类型处理器,确保类型处理器存在、配置正确,并且映射对象属性与数据库字段类型匹配,以解决该错误。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值