java.lang.IllegalStateException: commit already called in view pager with Tablayout

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (2)
  • 关注 (0)
  • 查看 (72)

我在我的项目中使用查看分页器与Tablayout。我不断收到crashlytics的崩溃报告。以下是堆栈跟踪。

java.lang.IllegalStateException: commit already called
   at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:624)
   at android.support.v4.app.BackStackRecord.commitAllowingStateLoss(BackStackRecord.java:620)
   at android.support.v4.app.FragmentStatePagerAdapter.finishUpdate(FragmentStatePagerAdapter.java:161)
   at android.support.v4.view.ViewPager.d(ViewPager.java:1105)
   at android.support.v4.view.ViewPager.d(ViewPager.java:951)
   at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
   at android.view.View.measure(View.java:15635)
   at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
   at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
   at android.view.View.measure(View.java:15635)
   at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:868)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
   at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
   at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4919)
   at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
   at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2200)
   at android.view.View.measure(View.java:15635)
   at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
   at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
   at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
   at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
   at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4872)
   at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
   at android.view.Choreographer.doCallbacks(Choreographer.java:579)
   at android.view.Choreographer.doFrame(Choreographer.java:548)
   at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
   at android.os.Handler.handleCallback(Handler.java:800)
   at android.os.Handler.dispatchMessage(Handler.java:100)
   at android.os.Looper.loop(Looper.java:194)
   at android.app.ActivityThread.main(ActivityThread.java:5371)
   at java.lang.reflect.Method.invokeNative(Method.java)
   at java.lang.reflect.Method.invoke(Method.java:525)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
   at dalvik.system.NativeStart.main(NativeStart.java)

这是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
    <android.support.v4.widget.DrawerLayout       
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/frame_container"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">

            <include
                android:id="@+id/toolbar"
                layout="@layout/toolbar" />

            <android.support.design.widget.TabLayout
                android:id="@+id/tabs"
                android:layout_width="match_parent"
                android:background="@color/spice_laddooblue"
                app:tabMode="scrollable"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.v4.view.ViewPager 
                    xmlns:android="http://schemas.android.com/apk/res/android"
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                </android.support.v4.view.ViewPager>

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@drawable/ic_share_white_24dp"
                android:layout_gravity="bottom|end"
                app:elevation="6dp"
                style="@style/floating_action_button"
                app:backgroundTint="@color/spice_laddooblue"
                app:pressedTranslationZ="12dp"/>
            </FrameLayout>


        </LinearLayout>

        <!--
              <View
            android:id="@+id/upper_view"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:visibility="visible" 
            android:layout_below = "@+id/pager"
            android:layout_above = "@+id/bannerView"/>
        -->

        <RelativeLayout
            android:id="@+id/bannerView"
            android:layout_width="match_parent"
            android:layout_height="58dp"
            android:layout_alignParentBottom="true"
            android:background="@drawable/curved_white_with_blue_border"
            android:visibility="gone">

            <TextView
                android:id="@+id/bannerText"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerInParent="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:padding="3dp"
                android:text="Banner"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/bannerImage"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentLeft="true"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:padding="3dp"
                android:scaleType="fitXY"
                android:visibility="gone" />

            <ImageView
                android:id="@+id/bannerClose"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_alignParentTop="true"
                android:layout_centerVertical="true"
                android:src="@drawable/cross_icon" />
        </RelativeLayout>
    </RelativeLayout>

    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_width="265dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#fff"
        android:choiceMode="singleChoice"
        android:divider="@null"
        android:dividerHeight="0dp"
        android:drawSelectorOnTop="true"
        android:groupIndicator="@null"
        android:scrollbars="@null" />

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

适配器代码

public class TabsPagerAdapter extends FragmentStatePagerAdapter {

    Context context;
    ViewPager pager;
    int count;

    public TabsPagerAdapter(FragmentManager fm, Context context, ViewPager pager ) {
        super(fm);
        this.context = context;
        this.pager = pager;

    }
    @Override
    public CharSequence getPageTitle(int position) {
        return ActivityMain.setdynamicTabsNames.get(position);
    }

    @Override
    public Fragment getItem(int index) {

        if (ActivityMain.setdynamicTabs.get(index).equalsIgnoreCase("HOME")) {
            return new FragmentIndex();
        else {
            return new FragmentDynamicTab(index);
        }
    }

        @Override
public int getCount() {
    return ActivityMain.setdynamicTabs.size();
}

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        // supertabLayout.destroyItemsetupWithViewPager(container, position, objectviewPager);
    }

    public void setCount(int count) {
        this.count = count;
    }
}

我在java代码中像这样设置tablayout

 if (viewPager != null) {
     viewPager.setAdapter(tabAdapter);
     tabLayout.setupWithViewPager(viewPager);
  }

我已经通过了所有可能的解决方案可用于此错误..但他们都没有工作。

提问于
用户回答回答于

这段代码看起来很熟悉。

它看起来像我在github中的项目MaterialAppBase,但这不是我的代码。

我读过你的代码,如果我没有错,你有一个活动,同时实现一个导航抽屉,实现一个TabLayout。

无论如何,这并不是一个好主意。我看到你正在处理一个可能有非常复杂布局的应用程序。

你所做的是违背Android设计指南,违背正常的用户体验设计。你不应该有两个滑动视图作为一个活动内的导航。NavigationDrawer和ViewPager之间的滑动手势可能会发生冲突。想想看,没有人会这样做。

不过,你们有很多人有理由这么做。如果您是这样做的,我会在您的代码中看到错误,但我不确定您的问题是否由此错误引起。

@Override
public Fragment getItem(int index) {

    if (ActivityMain.setdynamicTabs.get(index).equalsIgnoreCase("HOME")) {
        return new FragmentIndex();
    else {
        return new FragmentDynamicTab(index);
    }
}

问题是你不应该每次页面项目呈现时都创建一个新的片段。

你应该做的是:

  • 你应该提前创建你的片段。
  • 将你的片段添加到一个ArrayList或类似的东西。
  • 将此列表传递给适配器,以便它可以使碎片可重用。

看看我的代码适配器代码

用户回答回答于

显然commit()commitAllowingStateLoss()发生两次里面FragmentStatePagerAdapter。在这里,我从android源中发现:

这个适配器事务处理的唯一地方是FragmentStatePagerAdapter.finishUpdate()它的commitAllowingStateLoss()版本commit()。因此,finishUpdate()被称为两次。该方法从3个地方调用:

  • ViewPager.setAdapter();
  • ViewPager.dataSetChanged();
  • ViewPager.populate().

现在,finishUpdate()if (mCurTransaction != null)支票保护,并且在这张支票的内部mCurTransaction是空的。因此我们不能真正进行两次相同的交易。除非我们有多线程处理它。mCurTransaction字段是不稳定的,这使我想到,你可能直接或间接地从工作者线程调用了上面列出的方法之一,有时它会导致围绕mCurTransaction字段的竞争条件,从而导致第二个commitAllowingStateLoss()调用。

确保仅从主(UI)线程引用适配器和相关对象。

扫码关注云+社区