我试图在一个活动中实现DrawerLayout和TabLayout,但是我收到了这个错误:
java.lang.IllegalStateException:FragmentManager已在执行事务
这是 MainActivity.java
的代码// ...
import android.support.v4.app.FragmentManager;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
// ...
public class MainActivity extends AppCompatActivity implements
FeedFragment.OnFragmentInteractionListener,
ThreadsFragment.OnFragmentInteractionListener,
/* ... */ {
public static FragmentManager FM;
public static LayoutInflater inflater;
private static FrameLayout CONTENT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
FM = getSupportFragmentManager();
FragmentManager.enableDebugLogging(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/**
* Called when a drawer has settled in a completely closed state.
*/
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
ACTION_BAR.setTitle("Voog");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/**
* Called when a drawer has settled in a completely open state.
*/
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
ACTION_BAR.setTitle("Mina");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
CONTENT = (FrameLayout) findViewById(R.id.content_frame);
inflater = LayoutInflater.from(this);
try {
View tabs = inflater.inflate(R.layout.layout_tabs, null, false);
// if the call do this function is removed app executes without crashing
Tabs(tabs);
CONTENT.addView(tabs);
} catch (Exception e) {
Log.e("Tabs error", "Tabs error", e);
}
}
private static ViewPager VIEWPAGER;
private void Tabs(View view) {
VIEWPAGER = (ViewPager) view.findViewById(R.id.viewPager);
//set adapter to your ViewPager
try {
FM.executePendingTransactions();
FM.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
VIEWPAGER.setAdapter(new PagerAdapter(FM, 6));
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(VIEWPAGER);
TabLayout.Tab FeedTab = tabLayout.getTabAt(0);
FeedTab.setIcon(R.drawable.ic_whatshot_white_24dp);
TabLayout.Tab TodosTab = tabLayout.getTabAt(1);
TodosTab.setIcon(R.drawable.ic_insert_drive_file_white_24dp);
// ...
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
@Override
public void onTabSelected(TabLayout.Tab tab) {
VIEWPAGER.setCurrentItem(tab.getPosition());
}
@Override
public void onTabUnselected(TabLayout.Tab tab) {
}
@Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
} catch(Exception e) {
Log.e("Tabs error", "Tabs error", e);
}
}
activity_main.xml中:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<LinearLayout
android:orientation="vertical"
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_gravity="start"
android:layout_height="match_parent"
android:background="#ddd" >
<!-- ... -->
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
layout_tabs.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.design.widget.TabLayout
android:id="@+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
app:tabTextColor="@color/color_white_semitransparent"
app:tabSelectedTextColor="@android:color/white"
app:tabIndicatorColor="@android:color/white"
app:tabMode="scrollable"/>
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<me.zhanghai.android.materialprogressbar.MaterialProgressBar
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/mainprogress"
android:indeterminate="true"
android:visibility="visible"
style="@style/Widget.MaterialProgressBar.ProgressBar"
android:layout_gravity="center"/>
</FrameLayout>
这里是Logcat输出:
11-01 06:49:05.038 E/AndroidRuntime(3367): FATAL EXCEPTION: main
11-01 06:49:05.038 E/AndroidRuntime(3367): java.lang.IllegalStateException: FragmentManager is already executing transactions
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1626)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:166)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1614)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1421)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.LinearLayout.measureVertical(LinearLayout.java:712)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.LinearLayout.onMeasure(LinearLayout.java:605)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:1085)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:135)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.support.v7.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:389)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1421)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.LinearLayout.measureVertical(LinearLayout.java:712)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.widget.LinearLayout.onMeasure(LinearLayout.java:605)
11-01 06:49:05.038 E/AndroidRuntime(3367): at android.view.View.measure(View.java:15288)
// ...
和FragmentManager日志:
11-01 06:49:04.758 V/FragmentManager(3367): Run: BackStackEntry{417faa10}
11-01 06:49:04.758 V/FragmentManager(3367): add: FeedFragment{417a7340 id=0x7f0d00ab}
11-01 06:49:04.758 V/FragmentManager(3367): Allocated fragment index FeedFragment{417a7340 #0 id=0x7f0d00ab}
11-01 06:49:04.758 V/FragmentManager(3367): add: TodosFragment{41813ac8 id=0x7f0d00ab}
11-01 06:49:04.758 V/FragmentManager(3367): Allocated fragment index TodosFragment{41813ac8 #1 id=0x7f0d00ab}
11-01 06:49:04.758 V/FragmentManager(3367): moveto CREATED: FeedFragment{417a7340 #0 id=0x7f0d00ab}
11-01 06:49:04.758 V/FragmentManager(3367): moveto ACTIVITY_CREATED: FeedFragment{417a7340 #0 id=0x7f0d00ab}
11-01 06:49:04.768 V/FragmentManager(3367): moveto STARTED: FeedFragment{417a7340 #0 id=0x7f0d00ab}
11-01 06:49:04.768 V/FragmentManager(3367): moveto RESUMED: FeedFragment{417a7340 #0 id=0x7f0d00ab}
11-01 06:49:04.768 V/FragmentManager(3367): moveto CREATED: TodosFragment{41813ac8 #1 id=0x7f0d00ab}
11-01 06:49:04.768 V/FragmentManager(3367): moveto ACTIVITY_CREATED: TodosFragment{41813ac8 #1 id=0x7f0d00ab}
因此,这部分代码会导致问题:
private void Tabs(View view) {
VIEWPAGER = (ViewPager) view.findViewById(R.id.viewPager);
//set adapter to your ViewPager
try {
FM.executePendingTransactions();
FM.popBackStackImmediate(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
// ERROR
VIEWPAGER.setAdapter(new PagerAdapter(FM, 6));.
// ERROR
TabLayout tabLayout = (TabLayout) view.findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(VIEWPAGER);
// ...
} catch(Exception e) {
Log.e("Tabs error", "Tabs error", e);
}
}