ViewPager2崩溃

问题描述 投票:-2回答:1

我使用高级导航组件与BottomNavigationView,在一个标签页中,我有viewPager2第一次点击该标签页工作正常,但第二次来到该标签页应用程序一直崩溃。下面是crashlog,如果有人能帮忙,谢谢。

java.lang.IllegalArgumentException at androidx.core.util.Preconditions.checkArgument(Preconditions.java:36) at androidx.viewpager2.adapter.FragmentStateAdapter.onAttachedToRecyclerView(FragmentStateAdapter.java:140) at androidx.recyclerview.widget.RecyclerView.setAdapterInternal(RecyclerView. java:1206) at androidx.recyclerview.widget.RecyclerView.setAdapter(RecyclerView.java:1158) at androidx.viewpager2.widget.ViewPager2.setAdapter(ViewPager2.java:460) at com.****.ui.hom.history.HistoryFragment.setupAdapter(HistoryFragment.kt:25) at com.****.ui.hom.hom.HistoryFragment.viewSetup(HistoryFragment.java:25) at com.****.ui.hom.hom.HistoryFragment. viewSetup(HistoryFragment.kt:21) at com.****.base.BaseFragment.onViewCreated(BaseFragment.kt:37) at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:332) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager. at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1187) at androidx.fragment.app.FragmentManager.moveToState(FragmentManager.java:1497) at androidx.fragment.app.FragmentManager.dispatchStateChange(FragmentManager. java:2625) at androidx.fragment.app.FragmentManager.dispatchActivityCreated(FragmentManager.java:2577) at androidx.fragment.app.Fragment.exactivityCreated(Fragment.java:2722) at androidx.fragment.app.FragmentStateManager.activityCreated(FragmentStateManager.java:346) at androidx.fragment.app.FragmentManager. moveToState(FragmentManager.java:1188) at androidx.fragment.app.FragmentManager.addedFragments(FragmentManager.java:2224) at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:1997) at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager. java:1953) at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:1849) at androidx.fragment.app.FragmentManager$4.run(FragmentManager.java:413) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android. os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6940) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:537) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

这是我的fragment代码。

private val adapter by lazy {
        HistoryPagerAdapter(this)
    }

    override fun viewSetup() {
        binding.vpBuySell.adapter = adapter
        TabLayoutMediator(
            binding.tabBuySell,
            binding.vpBuySell,
            TabLayoutMediator.TabConfigurationStrategy { tab: TabLayout.Tab, i: Int ->
               tab.text = when (i) {
                    0 -> getString(R.string.buy)
                    1 -> getString(R.string.sell)
                    else -> getString(R.string.buy)
                }
            })
    }

这里是ui代码。

<androidx.constraintlayout.widget.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/blue_122e47">

    <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?actionBarSize"
        android:background="@color/blue_06233e"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:titleTextColor="@color/white">


        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/tvTitle"
            style="@style/ToolbarTitleTextStyle"
            android:text="@string/history" />

        <TextView
            android:id="@+id/btnExport"
            android:layout_width="wrap_content"
            android:layout_height="@dimen/_24sdp"
            android:layout_gravity="end"
            android:layout_marginEnd="@dimen/_8sdp"
            android:fontFamily="@font/helvetica_neue_medium"
            android:insetLeft="0dp"
            android:gravity="center"
            android:background="@drawable/shape_export_button"
            android:insetTop="0dp"
            android:insetRight="0dp"
            android:insetBottom="0dp"
            android:foreground="?selectableItemBackground"
            android:paddingBottom="@dimen/_2sdp"
            android:paddingStart="@dimen/_8sdp"
            android:paddingEnd="@dimen/_8sdp"
            android:text="@string/export"
            android:textAllCaps="false"
            android:textColor="@color/white"
            android:textSize="@dimen/_12ssp" />

    </androidx.appcompat.widget.Toolbar>


    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tabBuySell"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/blue_122e47"
        app:tabIndicatorFullWidth="false"
        app:tabIndicatorGravity="bottom"
        app:tabTextAppearance="@style/HistoryTabTextStyle"
        app:tabTextColor="@color/gray_697b8b"
        app:tabSelectedTextColor="@color/white"
        app:tabIndicatorHeight="@dimen/_2sdp"
        app:tabIndicatorColor="@color/blue_47cfff"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:tabGravity="start"
        app:tabMode="scrollable" />

    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/vpBuySell"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintTop_toBottomOf="@id/tabBuySell" />
</androidx.constraintlayout.widget.ConstraintLayout>

这是我的适配器代码。

class HistoryPagerAdapter(fragment: Fragment) : FragmentStateAdapter(fragment) {
override fun getItemCount(): Int {
    return 2
}
override fun createFragment(position: Int): Fragment {
    return when (position) {
        0 -> HistoryBuyFragment()
        1 -> HistorySellFragment()
        else -> HistoryBuyFragment()
    }
}

}

android android-recyclerview android-jetpack android-jetpack-navigation android-viewpager2
1个回答
4
投票

实际错误是适配器的懒惰初始化。我也不知道为什么会发生这种情况


1
投票

试试这个解决方案。

import androidx.viewpager2.adapter.FragmentStateAdapter

class HistoryPagerAdapter(activity: AppCompatActivity, private var itemCount: Int): FragmentStateAdapter(activity) {
override fun getItemCount(): Int {
    return 2
}
override fun createFragment(position: Int): Fragment {
    return when (position) {
        0 -> HistoryBuyFragment()
        1 -> HistorySellFragment()
        else -> HistoryBuyFragment()
    }
}

片段代码

private val TAB_ITEMS_COUNT = 2

adapter = HistoryPagerAdapter((activity as AppCompatActivity), TAB_ITEMS_COUNT)
binding.vpBuySell.adapter = adapter

TabLayoutMediator(binding.tabBuySell, binding.vpBuySell) { tab, position ->
    when(position)
    {
        0 -> tab.text = getString(R.string.buy)
        1 -> tab.text = getString(R.string.sell)
    }
}.attach()

binding.tabBuySell.addOnTabSelectedListener(object: TabLayout.OnTabSelectedListener
{
    override fun onTabReselected(tabItem: TabLayout.Tab?) {}
    override fun onTabUnselected(tabItem: TabLayout.Tab?) {}
    override fun onTabSelected(tabItem: TabLayout.Tab) {
        binding.vpBuySell.currentItem = tabItem.position
    }
})
© www.soinside.com 2019 - 2024. All rights reserved.