question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

ViewPager2 crash with Advance Navigation Component

See original GitHub issue

Hello guys , i am using Advance Navigation Component with BottomNavigationView, In One tab i have viewPager2 first clicked on tab is working fine but second time come on that tab application keep crashing. Below is crashlog if anyone can help thanks.

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)

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8

github_iconTop GitHub Comments

1reaction
goomii321commented, Nov 23, 2020

@vedo1608
I use Kotlin and fix crush by this way , maybe it could be helpful.

In ViewPager2 Fragment:

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
                              savedInstanceState: Bundle?): View? {
...
binding.viewPager2.adapter = ViewPagerAdapter(childFragmentManager,viewLifecycleOwner.lifecycle)
...
}

In Adapter:

class ViewPagerAdapter(manager: FragmentManager,lifecycle: Lifecycle):
    FragmentStateAdapter(manager,lifecycle) {
    override fun getItemCount():Int = TypeFilter.values().size

    override fun createFragment(position: Int): Fragment {
        return when(position) {
            0 -> FragmentA()
            1 -> FragmentB()
            else -> FragmentC()
        }
    }
}
1reaction
vedo1608commented, Nov 23, 2020

yes but that way you have to create adapter every time you open the screen in onViewCreated?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ViewPager2 crash - android - Stack Overflow
I am using Advance Navigation Component with BottomNavigationView.
Read more >
Migrate from ViewPager to ViewPager2 - Android Developers
If you currently use a ViewPager with a TabLayout object to display horizontal tabs for navigation, you need to refactor the TabLayout object ......
Read more >
ArcGIS map crashes under android ViewPager2
I tried to make an ArcGIS map work under an android ViewPager2 structure. The map diplays nicely but when I navigate away to...
Read more >
viewpager2 crash sometimes · Issue #103 - GitHub
When I return the activity containing viewpager2,crash sometimes Device:OPPO R9s Plus Android 6.0.1 Color OS version:V3.0 Log: java.lang.
Read more >
componentid:561920 status:open - Issue Tracker
P TYPE STATUS ID LAST MODIFIED P3 Bug Assigned 257099087 Nov 7, 2022 12:53PM P3 Bug Assigned 256270071 Nov 7, 2022 12:09PM P3 Bug Assigned 253842926...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found