片段到片段到片段崩溃
Fragment to fragment to fragment get crash
我有一个主 activity ,它包含很多片段。
activity 主要布局如下所示:
<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());
错误日志:
为什么我切换到第三个片段时崩溃了?我完全不知道。
我找到了崩溃函数function,因为我设置了class:
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);
现在我已经设置 public void switchFragment every Fragment class.
为什么我设置新的 CommonSwitch 会导致崩溃?
您的 Activity 状态为未维护。
在第二层使用 Activity 而不是 Fragment 。遵循 Android 标准。
根据标准,第三个片段应该是 Activity。否则你必须保持 MainActivity 状态。
谢谢
我有一个主 activity ,它包含很多片段。 activity 主要布局如下所示:
<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());
错误日志:
为什么我切换到第三个片段时崩溃了?我完全不知道。
我找到了崩溃函数function,因为我设置了class:
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);
现在我已经设置 public void switchFragment every Fragment class.
为什么我设置新的 CommonSwitch 会导致崩溃?
您的 Activity 状态为未维护。
在第二层使用 Activity 而不是 Fragment 。遵循 Android 标准。
根据标准,第三个片段应该是 Activity。否则你必须保持 MainActivity 状态。
谢谢