我有一个ViewPager活动,这个ViewPager包含3个片段。
我正在使用FragmentPagerAdapter创建片段,如下所示:
class ViewPagerUser(fragmentManager: FragmentManager, private val matchDetails: MatchDetails, private val context: Context) : FragmentPagerAdapter(fragmentManager) {
// Returns total number of pages
override fun getCount(): Int {
return NUM_ITEMS
}
// Returns the fragment to display for that page
override fun getItem(position: Int): Fragment? {
when (position) {
0 -> return FragmentDetailMatchInfoUser.newInstance(matchDetails)
1 -> return FragmentDetailMatchParticipantsUser.newInstance(matchDetails)
2 -> return FragmentDetailMatchWall.newInstance(matchDetails)
else -> return null
}
}
// Returns the page title for the top indicator
override fun getPageTitle(position: Int): CharSequence? {
when (position) {
0 -> return context.getString(R.string.info)
1 -> return context.getString(R.string.player)
2 -> return context.getString(R.string.wall)
else -> return null
}
}
companion object {
private val NUM_ITEMS = 3
}
}
这是我使用它的方式
if (matchDetails != null && (matchDetails.cancelled!! || screenType == App.USER || matchDetails.matchStatus == 40 || matchDetails.matchStatus == 50)) {
adapterViewPager = ViewPagerUser(supportFragmentManager, matchDetails, this)
} else if (screenType == App.ORGANIZER) {
adapterViewPager = ViewPagerOrganizer(supportFragmentManager, matchDetails!!, this)
}
viewPager?.adapter = adapterViewPager
viewPager?.offscreenPageLimit = 3
我认为这是在ViewPager中处理Fragments的最佳方式,但是我在Crashlytics上收到了很多关于“已添加片段”的报告,如下所示:
Fatal Exception: java.lang.IllegalStateException: Fragment already added: FragmentDetailMatchInfoUser{b17d64 #0 id=0x7f090551 android:switcher:2131297617:0}
at android.support.v4.app.FragmentManagerImpl.addFragment(Unknown Source)
at android.support.v4.app.BackStackRecord.addOp(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.executeOps(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(Unknown Source)
at android.support.v4.app.FragmentManagerImpl.execSingleAction(Unknown Source)
at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(Unknown Source)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(Unknown Source)
at android.support.v4.view.ViewPager.initViewPager(Unknown Source)
at android.support.v4.view.ViewPager.populate(Unknown Source)
at android.support.v4.view.ViewPager.onMeasure(Unknown Source)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.support.design.widget.CoordinatorLayout.onMeasureChild(Unknown Source)
at android.support.design.widget.HeaderScrollingViewBehavior.onMeasureChild(Unknown Source)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onMeasureChild(Unknown Source)
at android.support.design.widget.CoordinatorLayout.onMeasure(Unknown Source)
at android.view.View.measure(View.java:19883)
at android.support.constraint.ConstraintLayout.internalMeasureChildren(Unknown Source)
at android.support.constraint.ConstraintLayout.onMeasure(Unknown Source)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.support.v7.widget.ContentFrameLayout.onMeasure(Unknown Source)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1464)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:758)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:640)
at android.view.View.measure(View.java:19883)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:6087)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:185)
at com.android.internal.policy.DecorView.onMeasure(DecorView.java:689)
at android.view.View.measure(View.java:19883)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2293)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1384)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1637)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1272)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6408)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:874)
at android.view.Choreographer.doCallbacks(Choreographer.java:686)
at android.view.Choreographer.doFrame(Choreographer.java:621)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:860)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6165)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:888)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:778)
我在Stackoverflow中经历了与此问题相关的几乎所有问题,建议的答案无法帮助我...错误总会回来。
我有什么问题吗?
答案 0 :(得分:0)
这个报告可能是因为您每次ViewPager要求片段时都在创建片段的新实例。
您应该在创建ViewPager时尝试创建片段实例:
class ViewPagerUser(fragmentManager: FragmentManager, private val matchDetails: MatchDetails, private val context: Context) : FragmentPagerAdapter(fragmentManager) {
private val fragmentDetailMatchInfoUser = FragmentDetailMatchInfoUser.newInstance(matchDetails)
private val fragmentDetailMatchParticipantsUser = FragmentDetailMatchParticipantsUser.newInstance(matchDetails)
private val fragmentDetailMatchWall = FragmentDetailMatchWall.newInstance(matchDetails)
// Returns total number of page
override fun getCount(): Int {
return NUM_ITEMS
}
// Returns the fragment to display for that page
override fun getItem(position: Int): Fragment? {
when (position) {
0 -> return fragmentDetailMatchInfoUser
1 -> return fragmentDetailMatchParticipantsUser
2 -> return fragmentDetailMatchWall
else -> return null
}
}
// Returns the page title for the top indicator
override fun getPageTitle(position: Int): CharSequence? {
when (position) {
0 -> return context.getString(R.string.info)
1 -> return context.getString(R.string.player)
2 -> return context.getString(R.string.wall)
else -> return null
}
}
companion object {
private val NUM_ITEMS = 3
}
}
我有类似的问题,它帮助了我。希望它也能帮到你。
答案 1 :(得分:0)
使用isAdded()检查片段是否与活动分离。
if(!mFragmentDetailMatchInfoUser.isAdded())
{
return FragmentDetailMatchInfoUser.newInstance(matchDetails);
}
以及两个片段。
答案 2 :(得分:0)
我遇到了同样的问题,我通过向 FragmentPagerAdapter 添加第二个参数解决了这个问题:
class ViewPagerUser(fragmentManager: FragmentManager, private val matchDetails: MatchDetails, private val context: Context) : FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) {...}
更多: FragmentPagerAdapter(fragmentManager) 已弃用,必须改成 FragmentPagerAdapter(fragmentManager, BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT) 问题才能解决。