Android IllegalStateException片段已添加

时间:2017-06-13 10:26:25

标签: android android-fragments android-appcompat illegalstateexception

我的android应用程序出现异常。看起来当按下后退按钮并且我们想要弹出后台堆栈时(我认为这就是堆栈跟踪通知我的内容)会抛出非法状态异常。

AppVersion: 5.0.3
DeviceType: phone
DeviceDetails: SM-J510FN
SystemVersion: 6.0.1
Details: Fragment already added: CategoriesFragmentMobileImpl
{a6aafc9 #0 id=0x7f1200e0 boom.android.new_app.NewCategories.CategoriesFragment}
StackTrace: 

java.lang.IllegalStateException: Fragment already added: CategoriesFragmentMobileImpl{a6aafc9 #0 id=0x7f1200e0 boom.android.new_app.NewCategories.CategoriesFragment}
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1679)
at android.support.v4.app.BackStackRecord.executePopOps(BackStackRecord.java:777)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2353)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2098)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2008)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:710)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:158)
at android.app.ActivityThread.main(ActivityThread.java:7231)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
–

我正在使用支持库

compile 'com.android.support:appcompat-v7:25.1.1'

以下是我的代码中添加该片段的唯一位置:

 @Override
    protected void bindViews() {

        Fragment fragment = getSupportFragmentManager().findFragmentByTag(CategoriesFragment.TAG);
        if(fragment == null)
            fragment = CategoriesFragmentMobileImpl.newInstance(this);

        if(fragment.isAdded() == false) {
            getSupportFragmentManager().beginTransaction().
                    replace(R.id.main_fragment_container, fragment, CategoriesFragment.TAG)
                    .commit();
        }

        horizontalScrollView = (HorizontalScrollView)findViewById(R.id.breadcrumbs_ScrollView);
        breadcrumbsContainer = (LinearLayout) findViewById(R.id.breadcrumbs_container);
        second_toolbar = findViewById(R.id.second_toolbar);
        bindNavigationMenu(R.id.drawer_layout, navigationView, null);

    }

我正在查看Android中的一些错误,这个问题可能与以下问题有关: https://issuetracker.google.com/issues/36928785

以下是onBackPressed的代码

@Override
public void onBackPressed() {

    if(breadcrumbsContainer.getChildCount() > 0)
    {
    removeBreadCrumb(-1);
}
if(getSupportFragmentManager().getBackStackEntryCount() > 0)
{        
    getSupportFragmentManager().popBackStack();
}
else
{
    super.onBackPressed();
}

}

任何想法如何复制这个问题,甚至是堆栈跟踪的内容,以便我能更好地理解?

1 个答案:

答案 0 :(得分:0)

尝试使用replaceFragment()代替addFragment()!