我无法通过点击按钮找到成功替换另一个片段的方法

时间:2017-07-01 11:47:11

标签: android android-layout android-fragments

我经历了一些问题并按照我的想法进行了更改,但每次按下按钮替换碎片时应用程序仍会崩溃。片段具有通常的代码,仅用于简单的布局。在启动时,活动将显示我首先添加的片段。

这是我的主要活动:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        final FragmentLogin fl = new FragmentLogin();
        final FragmentRegistration fr = new FragmentRegistration();
        final android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
        final android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.fragcon, fr);
        fragmentTransaction.commit();


        Button button = (Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(fl.isVisible())
                {
                    fl.onActivityCreated(savedInstanceState);
                    fragmentTransaction.replace(R.id.fragcon, fr);

                    fragmentTransaction.commit();
                }
                else  {
                    fl.onActivityCreated(savedInstanceState);
                    fragmentTransaction.replace(R.id.fragcon, fl);
                    fragmentTransaction.commit();
                }
            }
        });
    }
}

这是我的activitymain.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.aditya.registrationnlogin.MainActivity">


<RelativeLayout
    android:id="@+id/fragcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_weight="1.8"
    android:layout_gravity="center_horizontal">

</RelativeLayout>

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_horizontal"
    android:text="SWITCH" />

</LinearLayout>

3 个答案:

答案 0 :(得分:2)

您必须为每个新事务调用fragmentManager.beginTransaction();,否则会抛出异常

 if(fl.isVisible())
     {
         //fl.onActivityCreated(savedInstanceState);
         fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
         fragmentTransaction.commit();
     }else{
         //fl.onActivityCreated(savedInstanceState);
         fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
         fragmentTransaction.commit();
     }

并使用setArgument来传递bundle而不会中断片段生命周期

答案 1 :(得分:0)

// use this it will work sure dude !!    
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final FragmentLogin fl = new FragmentLogin();
final FragmentRegistration fr = new FragmentRegistration();
final android.support.v4.app.FragmentManager fragmentManager = 
getSupportFragmentManager();
final android.support.v4.app.FragmentTransaction fragmentTransaction = 
fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragcon, fr);
fragmentTransaction.commit();

Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
    Fragment fragment = null;
    Class fragmentClass = null;
        if(fl.isVisible())
        {
            fragmentClass = FragmentRegistration.class;
        }
        else  {
            fragmentClass = FragmentLogin.class;
        }

   try {
        fragment = (Fragment) fragmentClass.newInstance();
     } catch (Exception e) {
        e.printStackTrace();
     }

  // Insert the fragment by replacing any existing fragment
    FragmentManager fragmentManager = getSupportFragmentManager();
    String backStateName = fragment.getClass().getName();
    boolean fragmentPopped = fragmentManager.popBackStackImmediate 
  (backStateName, 0);

  if (!fragmentPopped && fragmentManager.findFragmentByTag(backStateName) == 
null){ //fragment not in back stack, create it.
        FragmentTransaction ft = fragmentManager.beginTransaction();
        ft.replace(R.id.flContent, fragment, backStateName);
        ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
        ft.addToBackStack(backStateName);
        ft.commit();
    }
    }
});
}
}

答案 2 :(得分:0)

if(fl.isVisible())
     { 
         //put your argument like this
         Bundle args = new Bundle();
         args.putString(ARG_PARAM1, param1);
         fr.setArguments(args)

         fragmentManager.beginTransaction().replace(R.id.fragcon, fr);
         fragmentTransaction.commit();
     }else{
         //put your argument like this
         Bundle args = new Bundle();
         args.putString(ARG_PARAM1, param1);
         fr1.setArguments(args)
         fragmentManager.beginTransaction().replace(R.id.fragcon, fl);
         fragmentTransaction.commit();
     }