Android不允许转换多个片段:

时间:2014-04-21 20:50:39

标签: java android android-fragments

按照教程here和stackoverflow的一些帮助,我能够进行按钮切换,将当前片段替换为另一个。为了详细说明,我可以替换当前片段,但如果我希望用一个按钮替换之后的应用程序崩溃。我的代码:

MainActivity:

public class MainActivity extends Activity {

    public FragmentTransaction transaction = getFragmentManager().beginTransaction();
    public Fragment loginFragment = new LoginFragment();
    public Fragment mainFragment = new MainFragment();
    public Fragment settingsFragment = new SettingsFragment();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        if (savedInstanceState == null) {

            getFragmentManager().beginTransaction().add(R.id.container, loginFragment).commit();

        } else {

            getFragmentManager().beginTransaction().add(R.id.container, mainFragment).commit();
        }
    }

    public void goToMain(View v) {

        transaction.replace(R.id.container, mainFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public void goToSettings(View v) {

        transaction.replace(R.id.container, settingsFragment);
        transaction.addToBackStack(null);
        transaction.commit();
    }

    public static class LoginFragment extends Fragment {

        public LoginFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_login, container, false);

            return rootView;
        }
    }

    public static class MainFragment extends Fragment {

        public MainFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            return rootView;
        }
    }

    public static class SettingsFragment extends Fragment {

        public SettingsFragment() {

        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_settings, container, false);

            return rootView;
        }
    }
}

登录片段xml:

<RelativeLayout 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"
    tools:context="com.pointlight.kingdomcraft.MainActivity$LoginFragment" >

    <Button
        android:id="@+id/button_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="goToMain"
        android:text="@string/action_submit" />

</RelativeLayout>

主片段xml:

<RelativeLayout 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"
    tools:context="com.pointlight.kingdomcraft.MainActivity$MainFragment" >

    <Button
        android:id="@+id/button_settings"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:onClick="goToSettings"
        android:text="@string/action_settings" />
</RelativeLayout>

正如您所看到的,两个不同事务的代码完全相同...为什么android不允许多次切换片段?

编辑:无论出于何种原因,我的logcat都是空的

1 个答案:

答案 0 :(得分:2)

您在事务上调用commit(),但在新beginTransaction()命令之前未调用replace(...)

编辑:也许这不清楚。我的意思是:

public void goToMain(View v) {
    FragmentTransaction transaction = getFragmentManager().beginTransaction();
    transaction.replace(R.id.container, mainFragment);
    transaction.addToBackStack(null);
    transaction.commit();
}

等等。您无法重复使用您的交易,因此请勿保存。创建一个新的。