碎片碎片碎片崩溃

时间:2017-01-11 08:31:50

标签: android android-fragments fragmentmanager

我有一个主要活动,它包含许多片段。 主要活动布局如下所示:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.user.taiwandigestionsociety_v11.MainActivity">


    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF">


<include layout="@layout/toolbar"/>
        //switch fragment over here
        <FrameLayout
            android:id="@+id/mainFrame"
            android:layout_gravity="end"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </FrameLayout>

    </android.support.design.widget.AppBarLayout>


    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@android:color/transparent"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header">
    </android.support.design.widget.NavigationView>


</android.support.v4.widget.DrawerLayout>

然后我点击它将改为第一个片段的抽屉项目:

@Override
    public void onClick(View view) {
        int id = view.getId();
    if (id == R.id.activitiesContents) {
            //change to first fragment successful
            switchFragment(ActivityList.newInstance());
            toolbar.setTitle(R.string.activitiesContents);
            drawerLayout.closeDrawer(GravityCompat.START);
        }
    }

我的切换片段功能:

public void switchFragment(Fragment fragment) {
        FragmentManager manager = getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.mainFrame, fragment, null);
        //transaction.addToBackStack(null);
        transaction.commit();
    }

然后我点击第一个片段中的按钮,它也会变为第二个片段:

//take me to second fragment succeed
switchFragment(ActivityHomePage.newInstance());

最后我点击了第二个片段中的按钮,它崩溃了

//i want to switch to third fragment , just the same with previous
switchFragment(NewsInformation.newInstance());

错误日志: error

当我切换到第三个片段时,为什么我会崩溃?我完全不知道。

我找到了崩溃函数函数,因为我设置了类:

public class CommonSwitch extends Fragment {
    //switch Fragment
    public void switchFragment(Fragment fragment) {
        FragmentManager manager = getActivity().getSupportFragmentManager();
        FragmentTransaction transaction = manager.beginTransaction();
        transaction.replace(R.id.mainFrame, fragment, null);
        //transaction.addToBackStack(null);
        transaction.commit();
    }
}

我尝试将其称为新的CommonSwitch.switchFragment(goToFragment);

现在我已经为每个Fragment类设置了public void switchFragment。

为什么我设置新的CommonSwitch会导致崩溃?

1 个答案:

答案 0 :(得分:0)

您的活动状态未维护。

在第二层上使用Activity而不是Fragment。遵循Android标准。

根据标准,第三片段应该是活动。否则你必须保持MainActivity状态。

由于

相关问题