错误在Android的碎片addToBackStack

错误在Android的碎片addToBackStack

本文介绍了"犯已经称为QUOT;错误在Android的碎片addToBackStack的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创造了一个家庭的活动,其中包括一个导航抽屉的onclick与片段。我已经包括 fragmentTransaction.addToBackStack(空).commit(); 的片段交易code回去previous页面。

我要的要求是 NavigationDrawer - >片段1 - > Fragment2 - >在后退按钮preSS - >片段1(上后退按钮pressed) - 方式> MainActivity

不过,现在我只能移动到片段1 ,但不能移动到 Fragment2
它显示了一个错误 java.lang.IllegalStateException:已提交名为

在我的MainActivity

  @燮pressWarnings(StatementWithEmptyBody)
@覆盖
公共布尔onNavigationItemSelected(菜单项项){
    FragmentManager fragmentManager = getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    //处理导航视图项点击这里。
    INT ID = item.getItemId();
    RelativeLayout的mainLayout =(RelativeLayout的)findViewById(R.id.mainlayout);  如果(ID == R.id.nav_project){
        ProjectFragment片段=新ProjectFragment();
        mainLayout.removeAllViews();
        fragmentTransaction.add(R.id.mainlayout,片段);
        fragmentTransaction.addToBackStack(空).commit();    }    DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    返回true;
}
 @覆盖
    公共无效onBack pressed(){
        DrawerLayout抽屉=(DrawerLayout)findViewById(R.id.drawer_layout);
        如果(drawer.isDrawerOpen(GravityCompat.START)){
            drawer.closeDrawer(GravityCompat.START);
        }其他{
            super.onBack pressed();
        }
    }

我的第一块碎片

 进口android.os.Bundle;
进口android.support.annotation.Nullable;
进口android.support.v4.app.Fragment;
进口android.support.v4.app.FragmentManager;
进口android.support.v4.app.FragmentTransaction;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;
进口android.widget.Button;
进口android.widget.ImageView;
进口android.widget.RelativeLayout;
/ **
 *一个简单的{@link片段}子类。
 * /
公共类LoginFragment扩展片段{
    按钮登入,登录;
    ImageView的标志;    @Nullable
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
        视图V = inflater.inflate(R.layout.fragment_login,集装箱,FALSE);
        FragmentManager fragmentManager = getFragmentManager();
        最后FragmentTransaction fragmentTransaction =
                fragmentManager.beginTransaction();
        最终的RelativeLayout mainLayout =(RelativeLayout的)getActivity()findViewById(R.id.mainlayout)。
        登入=(按钮)v.findViewById(R.id.button2);        signin.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                SignUpFragment片段=新SignUpFragment();
                mainLayout.removeAllViews();
                fragmentTransaction.add(R.id.mainlayout,片段);
                fragmentTransaction.commit();
                fragmentTransaction.addToBackStack(空).commit();
            }
        });
        登录=(按钮)v.findViewById(R.id.button);
        login.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                LoginEnterFragment片段=新LoginEnterFragment();
                mainLayout.removeAllViews();
                fragmentTransaction.add(R.id.mainlayout,片段);
                fragmentTransaction.commit();
                fragmentTransaction.addToBackStack(空).commit();
            }
        });
        LOGO =(ImageView的)v.findViewById(R.id.imageView);        返回伏;
    }}

我的第三个片段

 进口android.os.Bundle;
进口android.support.v4.app.Fragment;
进口android.view.LayoutInflater;
进口android.view.View;
进口android.view.ViewGroup;/ **
 *一个简单的{@link片段}子类。
 * /
公共类LoginEnterFragment扩展片段{    公共LoginEnterFragment(){
        //必需的空公共构造
    }
    @覆盖
    公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
                             捆绑savedInstanceState){
        //充气的布局该片段
        返回inflater.inflate(R.layout.fragment_login_enter,集装箱,FALSE);
    }
}

我的logcat

  11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:致命异常:主要
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:工艺:com.example.rubin,PID:676
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:java.lang.IllegalStateException:提交已经叫
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:621)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:613)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在com.example.root5solutions.mirrealtors.LoginFragment $ 2.onClick(LoginFragment.java:53)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.view.View.performClick(View.java:4780)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.view.View $ PerformClick.run(View.java:19866)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.os.Handler.handleCallback(Handler.java:739)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.os.Handler.dispatchMessage(Handler.java:95)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.os.Looper.loop(Looper.java:135)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在android.app.ActivityThread.main(ActivityThread.java:5254)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在java.lang.reflect.Method.invoke(本机方法)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在java.lang.reflect.Method.invoke(Method.java:372)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:903)
11-01 23:44:17.237 676-676 / com.example.rubin E / AndroidRuntime:在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)


解决方案

Because calling commit() method two time in LoginFragment Fragment on signin and login Button click :

fragmentTransaction.commit();
fragmentTransaction.addToBackStack(null).commit();

lines causing issue.

Use addToBackStack method as:

fragmentTransaction.addToBackStack(null).commit();

这篇关于"犯已经称为QUOT;错误在Android的碎片addToBackStack的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 02:17