在实际开发中经常会遇到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);
在子项添加前,先获取子项的父控件,然后移除父控件中的子项,例如:
//留言板 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);
原因:当前对客户端的响应已经结束,不能在响应已经结束(或说消亡)后再向客户端(实际上是缓冲区)输出任何内容。
解决方法:在退出的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()拼接。