android / views-widgets-samples

Multiple samples showing the best practices in views-widgets on Android.
Apache License 2.0
5.07k stars 3.01k forks source link

Exception java.lang.IllegalStateException #243

Open imadulhassan opened 2 years ago

imadulhassan commented 2 years ago

I was working with viewpager2 with tab layout . I am facing repeated crash due to IllegalStateException on playconsole.

Exception getting on Google Console

    at androidx.fragment.app.FragmentManager.ensureExecReady (FragmentManager.java:1931)
    at androidx.fragment.app.FragmentManager.execSingleAction (FragmentManager.java:1967)
    at androidx.fragment.app.BackStackRecord.commitNow (BackStackRecord.java:305)
    at androidx.viewpager2.adapter.FragmentStateAdapter.removeFragment (FragmentStateAdapter.java:464)
    at androidx.viewpager2.adapter.FragmentStateAdapter.gcFragments (FragmentStateAdapter.java:228)
    at androidx.viewpager2.adapter.FragmentStateAdapter.restoreState (FragmentStateAdapter.java:569)
    at androidx.viewpager2.widget.ViewPager2.restorePendingState (ViewPager2.java:350)
    at androidx.viewpager2.widget.ViewPager2.dispatchRestoreInstanceState (ViewPager2.java:375)
    at android.view.ViewGroup.dispatchRestoreInstanceState (ViewGroup.java:3829)
    at android.view.View.restoreHierarchyState (View.java:18613)
    at androidx.fragment.app.Fragment.restoreViewState (Fragment.java:639)
    at androidx.fragment.app.Fragment.restoreViewState (Fragment.java:3010)
    at androidx.fragment.app.Fragment.performActivityCreated (Fragment.java:3001)
    at androidx.fragment.app.FragmentStateManager.activityCreated (FragmentStateManager.java:580)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState (FragmentStateManager.java:285)
    at androidx.fragment.app.FragmentStore.moveToExpectedState (FragmentStore.java:112)
    at androidx.fragment.app.FragmentManager.moveToState (FragmentManager.java:1647)
    at androidx.fragment.app.FragmentManager.dispatchStateChange (FragmentManager.java:3128)
    at androidx.fragment.app.FragmentManager.dispatchActivityCreated (FragmentManager.java:3072)
    at androidx.fragment.app.FragmentController.dispatchActivityCreated (FragmentController.java:251)
    at androidx.fragment.app.FragmentActivity.onStart (FragmentActivity.java:501)
    at androidx.appcompat.app.AppCompatActivity.onStart (AppCompatActivity.java:246)
    at android.app.Instrumentation.callActivityOnStart (Instrumentation.java:1392)
    at android.app.Activity.performStart (Activity.java:7252)
    at android.app.ActivityThread.handleStartActivity (ActivityThread.java:2970)
    at android.app.servertransaction.TransactionExecutor.performLifecycleSequence (TransactionExecutor.java:180)
    at android.app.servertransaction.TransactionExecutor.cycleToPath (TransactionExecutor.java:165)
    at android.app.servertransaction.TransactionExecutor.executeLifecycleState (TransactionExecutor.java:142)
    at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:70)
    at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1831)
    at android.os.Handler.dispatchMessage (Handler.java:106)
    at android.os.Looper.loop (Looper.java:201)
    at android.app.ActivityThread.main (ActivityThread.java:6826)
    at java.lang.reflect.Method.invoke (Method.java)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:547)
    at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:873)```

**Here is my working Code in Fragment** 

TabLayoutMediator(binding.tablayout,binding.viewpagerSeeAll) { tab, position -> tab.text= " ${Constants.categories.get(position).displayName}" }.attach() binding.crossAdBackground.setOnClickListener { if (Constants.isUserFree()) { Util.proPopup(true, requireContext()!!, firabaseAnalytics, editActivityUtils) } else prefManager.let { Util.goToProMethod(requireActivity(), it) } }


**Viewpager Adapter Code** 

inner class ViewPagerAdapter(categories: ArrayList, fragment:Fragment): FragmentStateAdapter(fragment) { var categories: ArrayList = categories override fun getItemCount(): Int { return categories.size

    }
    override fun createFragment(position: Int): Fragment {
        val fragment = ViewPagerItemFragment()
        val args = Bundle()
        args.putParcelable(TemplateCatDetail.ARG_PARAM2, categories.get(position))
        args.putInt(TemplateCatDetail.ARG_PARAM3, position)
        args.putString(
            TemplateCatDetail.ARG_PARAM4,
            Constants.categories[position].displayName
        )
        fragment.arguments = args
        return fragment
    }
}