IllegalStateException:该片段已添加到tabhost片段中 [英] IllegalStateException: Fragment already added in the tabhost fragment

查看:139
本文介绍了IllegalStateException:该片段已添加到tabhost片段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FATAL EXCEPTION: main
Process: com.example.loan, PID: 24169
java.lang.IllegalStateException: Fragment already added: FormFragment{428f10c8 #1 id=0x7f050055 form}
    at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1192)
    at android.support.v4.app.BackStackRecord.popFromBackStack(BackStackRecord.java:722)
    at android.support.v4.app.FragmentManagerImpl.popBackStackState(FragmentManager.java:1533)
    at android.support.v4.app.FragmentManagerImpl$2.run(FragmentManager.java:489)
    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1484)
    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:450)
    at android.os.Handler.handleCallback(Handler.java:733)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:136)
    at android.app.ActivityThread.main(ActivityThread.java:5068)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:515)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
    at dalvik.system.NativeStart.main(Native Method)

因此,我有一个使用tabhost构建的android应用.总共有三个选项卡,在tab2中,有一个用于在tab2中进行片段事务的按钮(正在片段活动中调用该函数)

So, I have an android app that build with the tabhost. There are three tabs in total, in the tab2, there is a button to make the fragment transaction in tab2 (which is calling the function in the fragment activity)

FragmentTransaction t = getSupportFragmentManager().beginTransaction();
        t.replace(R.id.realtabcontent, mFrag);
        t.addToBackStack(null);
        t.commit();

如果我这样跑,那就有例外了:

There is exception if I run like this:

  1. 在tab2内,我按下按钮以更改片段
  2. 转到其他标签(例如标签1或标签3)
  3. 按下后退按钮
  4. 引发异常

该如何解决?感谢您的帮助

How to fix that? Thanks for helping

推荐答案

这种情况发生在我们尝试在关闭前两次添加相同的片段或DialogFragment

This happens when we try to add same fragment or DialogFragment twice before dismissing,

只需拨打电话

if(mFragment.isAdded())
{
     return; //or return false/true, based on where you are calling from
}

话虽如此,我看不出为什么要删除旧片段并再次添加相同的片段,因为我们可以通过简单地将参数传递给片段内部的方法来更新UI/数据

Having said that, I don't see any reason why to remove old fragment and add the same fragment again since we can update the UI/data by simply passing parameters to the method inside the fragment

这篇关于IllegalStateException:该片段已添加到tabhost片段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆