我在项目中使用View寻呼机和Tablayout。我不断收到关于崩解的崩溃报告。下面是堆栈跟踪。
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);
}
我已经通过了所有可能的解决方案来解决这个错误。但是他们都没有工作。我无法追查导致此异常的原因。我们将不胜感激。
答案 0 :(得分:3)
显然commit()
或commitAllowingStateLoss()
在FragmentStatePagerAdapter
内发生两次。这是我从android源代码中发现的:
提交此适配器事务的唯一位置是FragmentStatePagerAdapter.finishUpdate()
及commitAllowingStateLoss()
commit()
版本的finishUpdate()
。因此,ViewPager.setAdapter();
被调用两次。从3个地方调用此方法:
ViewPager.dataSetChanged();
ViewPager.populate().
finishUpdate()
现在,if (mCurTransaction != null)
受mCurTransaction
检查保护,在此检查中mCurTransaction
已取消。因此,我们无法真正提交两次相同的交易。除非我们有多个线程正在处理它。 mCurTransaction
字段不易变,这使我想到您可能(直接或间接)从工作线程调用上面列出的方法之一,有时会导致{{1 }}字段,这会导致第二次commitAllowingStateLoss()
调用。
确保仅从主(UI)线程引用适配器和相关对象。
答案 1 :(得分:-1)
这段代码看起来非常熟悉。
它看起来像我的项目MaterialAppBase在github中,但这不是我的代码。
我读了你的代码,如果我没有错,你有一个同时实现导航抽屉的活动,就会实现一个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);
}
}
问题是每次页面项呈现时都不应创建新片段。
你应该做的是:
查看我的代码the adapter code