I am using Advance Navigation Component with BottomNavigationView.
In one tab I have ViewPager2. When I clicked on the tab for the first time, it worked fine.
Although the second time, come on that tab application keep crashing. Below is the crash log. How can I fix this?
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.home.history.HistoryFragment.setupAdapter(HistoryFragment.kt:25)
at com..ui.home.history.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.moveToState(FragmentManager.java:1356)
at androidx.fragment.app.FragmentManager.moveFragmentToExpectedState(FragmentManager.java:1434)
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.performActivityCreated(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.addAddedFragments(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)
Here is my code for the 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)
}
})
}
Here is the UI code:
<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>
Here is my adapter code:
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()
}
}
}