Android 碎片到碎片到碎片崩溃
我有一个主要的活动,它包含很多片段。 主活动布局如下所示:Android 碎片到碎片到碎片崩溃,android,android-fragments,fragmentmanager,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
<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>
我的开关片段函数:
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());
错误日志:
为什么我切换到第三个片段时会崩溃?我完全不知道
我找到了崩溃函数,因为我设置了类:
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();
}
}
我试着把它称为新的公共开关。开关碎片(goToFragment)
现在我已经为每个片段类设置了公共void switchFragment
为什么我在设置新的CommonSwitch时会导致崩溃?您的活动状态为“未维护”。
在第二层,使用活动而不是片段。遵循Android标准
根据标准,第三个片段应该是活动。否则,您必须保持MainActivity状态
谢谢您的错误日志没有提供错误发生的线索!。。更新它
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();
}
}