出现这个问题的原因是因为使用的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的更多相关文章

  1. maven单元测试报java.lang.IllegalStateException: Failed to load ApplicationContext

    报这个异常java.lang.IllegalStateException: Failed to load ApplicationContext的时候,通常是因为applicationContent.x ...

  2. response.sendRedirect 报 java.lang.IllegalStateException 异常的解决思路

    今天在进行代码开发的时候,出现了 java.lang.IllegalStateException异常,response.sendRedirect("./DEFAULT.html") ...

  3. [Android Pro] java.lang.IllegalStateException: Fragment(XXFragment) not attached to Activity异常

    转载:http://blog.csdn.net/winson_jason/article/details/20357435 下边两个问题,是在开发中碰到的一些关于Fragment的偶发性的问题,今天时 ...

  4. 如何进行fragment中的来回切换?

    本文选自StackOverflow(简称:SOF)精选问答汇总系列文章之一,本系列文章将为读者分享国外最优质的精彩问与答,供读者学习和了解国外最新技术,本文为大家讲解如何进行fragment中的来回切 ...

  5. eclipse启动报错java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' befo

    报错: java.lang.IllegalStateException: LifecycleProcessor not initialized - call 'refresh' before invo ...

  6. eclipse 运行报java.lang.OutOfMemoryError: PermGen space解决方法

    一.在window下eclipse里面Server挂的是tomcat6,一开始还是以为,tomcat配置的问题,后面发现,配置了tomcat里面的catalina.bat文件,加入 set JAVA_ ...

  7. Tomcat报java.lang.ClassNotFoundException: 1catalina.org.apache.juli.FileHandler

    最近在生产环境部署Tomcat的时候,在启动的时候,在控制台报"java.lang.ClassNotFoundException: 1catalina.org.apache.juli.Fil ...

  8. idea生成JAVADOC 报java.lang.IllegalArgumentException解决方案[终极]

    idea生成javadoc文档,总是会报  java.lang.IllegalArgumentException     at sun.net.www.ParseUtil.decode(ParseUt ...

  9. Eclipse下执行main函数报java.lang.NoClassDefFoundError的解决

    今天执行eclipse下的一个java类,无论run还是debug,都报java.lang.NoClassDefFoundError.而且把main中函数都注释掉,执行还是报一样的错. 检查了一下这个 ...

随机推荐

  1. 函数page_cur_search_with_match

    /****************************************************************//** Searches the right position fo ...

  2. BZOJ_1007_ [HNOI2008]_水平可见直线_(单调栈+凸包)

    描述 http://www.lydsy.com/JudgeOnline/problem.php?id=1007 给出一些直线,沿着y轴从上往下看,能看到多少条直线. 分析 由于直线相交,会遮挡住一些直 ...

  3. [HDU 4549] M斐波那契数列

    M斐波那契数列 Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 65535/32768 K (Java/Others)Total Sub ...

  4. Chrome的隐身模式

    先来说说隐身模式的启用方法吧 1.键盘快捷:Ctrl + Shift + N. 2.在Windows7下的任务栏处,右击“Chrome”图标,会出一个下拉菜单,点击“新建隐身窗口”. 3.你还可以在一 ...

  5. useradd adduser linux创建用户、设置密码、修改用户、删除用户

    创建用户.设置密码.修改用户.删除用户: useradd testuser 创建用户testuser passwd testuser 给已创建的用户testuser设置密码 说明:新创建的用户会在/h ...

  6. oracle to_date函数(转载)

    TO_DATE格式(以时间:2007-11-02   13:45:25为例)           Year:              yy two digits 两位年                ...

  7. ACM2028

    Lowest Common Multiple Plus Problem Description 求n个数的最小公倍数.   input 输入包含多个测试实例,每个测试实例的开始是一个正整数n,然后是n ...

  8. 2014年Tizen开发者峰会上海征稿启事!

    本次征稿面向大中华用户: “Tizen开发者,应用程序开发人员.isv平台设计师.运营商.厂商.硬件厂商.软件厂商,开源爱好者,和从事Tizen的工作人员” 2014年Tizen开发者峰会 这一次,亚 ...

  9. Re-enable Alcatraz on Xcode 6.3.2 or newer

    I’ve been using Alcatraz to manage Xcode plug-ins for some time now. After updating to Xcode 6.3.2 a ...

  10. 最简单实现跨域的方法:用 Nginx 反向代理

    本文作者: 伯乐在线 - 良少 .未经作者许可,禁止转载!欢迎加入伯乐在线 专栏作者. 什么是跨域 跨域,指的是浏览器不能执行其他网站的脚本.它是由浏览器的同源策略造成的,是浏览器对javascrip ...