Fragment碎片频繁来回切换的时候报java.lang.IllegalStateException: No activity
出现这个问题的原因是因为使用的transcation.replace(fragmentTwo);的方式进行碎片切换的。
解决方案是使用add和show、hide方法组合实现碎片的切换(应该是显示、隐藏)。
我这里使用的是RadioButton控件的Id值。
/**
* 切换碎片:使用show和hide以及add,不要使用replace(频繁来回切换的时候容易发生内存溢出的错误)
*/
public void onTabSelected(int radioGroupId) {
//开启一个事务
FragmentTransaction transcation = fragmentManager.beginTransaction();
//设置淡入淡出效果
transcation.setCustomAnimations(android.R.animator.fade_in,android.R.animator.fade_out);
//隐藏全部碎片
hideFragments(transcation);
//有选择的显示碎片
switch (radioGroupId) { case R.id.home:
if(fragmentOne == null){
fragmentOne = new FragmentOne();
transcation.add(R.id.center_layout, fragmentOne);
}else{
transcation.show(fragmentOne);
}
break; case R.id.category:
if(fragmentTwo == null){
fragmentTwo = new FragmentTwo();
transcation.add(R.id.center_layout, fragmentTwo);
}else{
transcation.show(fragmentTwo);
}
break; case R.id.collect:
if(fragmentThree == null){
fragmentThree = new FragmentThree();
transcation.add(R.id.center_layout, fragmentThree);
}else{
transcation.show(fragmentThree);
}
break; case R.id.setting:
if(fragmentFour == null){
fragmentFour = new FragmentFour();
transcation.add(R.id.center_layout, fragmentFour);
}else{
transcation.show(fragmentFour);
}
break; default:
break;
}
transcation.commitAllowingStateLoss();
} /**隐藏全部碎片
* 需要注意:不要在OnResume方法中实例化碎片,因为先添加、显示,才可以隐藏。否则会出现碎片无法显示的问题*/
private void hideFragments(FragmentTransaction transaction) {
if (null != fragmentOne) {
transaction.hide(fragmentOne);
}
if (null != fragmentTwo) {
transaction.hide(fragmentTwo);
}
if (null != fragmentThree) {
transaction.hide(fragmentThree);
}
if (null != fragmentFour) {
transaction.hide(fragmentFour);
}
}
需要注意,不要在OnResume方法中实例化碎片。
// 存放底部菜单的各个RadioButton的Id值
private int[] radioButtonIds = {R.id.home,R.id.category,R.id.collect,R.id.setting}; @Override
protected void onResume() { //切换碎片
onTabSelected(radioButtonIds[0]); super.onResume();
}
Fragment碎片频繁来回切换的时候报java.lang.IllegalStateException: No activity的更多相关文章
- maven单元测试报java.lang.IllegalStateException: Failed to load ApplicationContext
报这个异常java.lang.IllegalStateException: Failed to load ApplicationContext的时候,通常是因为applicationContent.x ...
- response.sendRedirect 报 java.lang.IllegalStateException 异常的解决思路
今天在进行代码开发的时候,出现了 java.lang.IllegalStateException异常,response.sendRedirect("./DEFAULT.html") ...
- [Android Pro] java.lang.IllegalStateException: Fragment(XXFragment) not attached to Activity异常
转载:http://blog.csdn.net/winson_jason/article/details/20357435 下边两个问题,是在开发中碰到的一些关于Fragment的偶发性的问题,今天时 ...
- 如何进行fragment中的来回切换?
本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术,本文为大家讲解如何进行fragment中的来回切 ...
- eclipse启动报错java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' befo
报错: java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invo ...
- eclipse 运行报java.lang.OutOfMemoryError: PermGen space解决方法
一.在window下eclipse里面Server挂的是tomcat6,一开始还是以为,tomcat配置的问题,后面发现,配置了tomcat里面的catalina.bat文件,加入 set JAVA_ ...
- Tomcat报java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler
最近在生产环境部署Tomcat的时候,在启动的时候,在控制台报"java.lang.ClassNotFoundException: 1catalina.org.apache.juli.Fil ...
- idea生成JAVADOC 报java.lang.IllegalArgumentException解决方案[终极]
idea生成javadoc文档,总是会报 java.lang.IllegalArgumentException at sun.net.www.ParseUtil.decode(ParseUt ...
- Eclipse下执行main函数报java.lang.NoClassDefFoundError的解决
今天执行eclipse下的一个java类,无论run还是debug,都报java.lang.NoClassDefFoundError.而且把main中函数都注释掉,执行还是报一样的错. 检查了一下这个 ...
随机推荐
- 逻辑运算符&& 用法解释
A&&B 首先判断A,A成功然后判断B:A不成功则结束判断,返回false;
- ASP.NET MVC Overview
ASP.NET MVC Overview The Model-View-Controller (MVC) architectural pattern separates an application ...
- BZOJ1334: [Baltic2008]Elect
1334: [Baltic2008]Elect Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 386 Solved: 201[Submit][Sta ...
- Xcode7创建纯代码空白工程
0: Create a new project with 'single view controller' A: Xcode Settings 1: migrate launch image B: ...
- 小图用 imageNamed: 大图用 dataWithContentsOfFile:options
1down voteaccepted If you're loading images like:[UIImage imageNamed:@"myImage.png"];Then ...
- json-lib反序列化时(JSONObject.toBean),时间类型为空的处理
需求: 在我们的项目里希望JsonString传入日期类型值为空时,JSONObject.toBean时可以将Java对象的该日期属性设为null. 解决过程: json-lib反序列化Json字符串 ...
- [Irving]SqlServer 拆分函数用法
drop function dbo.split 删除自定义函数 dbo.split 函数名 函数 dbo.splitcreate function dbo.split(@c varchar(100 ...
- CentOS6.x安装配置nginx [转]
博文来源:http://leyewen.blog.163.com/ nginx安装 nginx的官网:http://nginx.org/ 相应下载页面:http://nginx.org/en/ ...
- [POJ2348]Euclid's Game
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8313 Accepted: 3374 Description Two p ...
- bzoj 1951 [Sdoi2010]古代猪文(数论知识)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1951 [思路] 一道优(e)秀(xin)的数论题. 首先我们要求的是(G^sigma{ ...