ViewPager2 must not be null in NavigationDrawer and SwipeViewTabs

I am trying to link my Navigation Drawer with SwipeView Tabs, the problem is that the logcat tells me that my Viewpager must not be null, I have tried to solve this problem in many ways but could not.

PageAdaper.kt

  class ViewPagerAdapter(fragmanetActivity: TabFragment): FragmentStateAdapter(fragmanetActivity) {
    
        override fun getItemCount(): Int = 3
    
        override fun createFragment(position: Int): Fragment {
            when (position) {
                0 -> return FirstFragment()   
                1 -> return SecondFragment()
                2 -> return ThirdFragment()
            }
            return Fragment()
        }
    }

Fragment

class TabFragment : Fragment() {
    private val adapter by lazy { ViewPagerAdapter(this) }
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        val x = inflater.inflate(R.layout.contain_main, container, false)

        pager.adapter = adapter // This is the error

        TabLayoutMediator(tab_layout, pager) { tab, position ->
            when (position) {
                0 -> tab.text = "option1"
                1 -> tab.text = "option2"
                2 -> tab.text = "option3"
            }
        }.attach()
        return x
   }
}

contain_main.xml

I linked this file with ( class TabFragment : Fragment() )

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.google.android.material.tabs.TabLayout
        android:id="@+id/tab_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabSelectedTextColor="#E91E63" />
    <androidx.viewpager2.widget.ViewPager2
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />
</LinearLayout>

In your class TabFragment you are trying to access pager view. Since you are in the onCreateView method, synthetic doesn’t know how to access view and give you reference on pager.
You can do that in onViewCreated and later callbacks, or you can use val x to access pager.

2 Likes

I did it this way and it worked, any suggestion I would appreciate

class TabFragment : Fragment() {
    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.contain_main, container, false)

    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        val adapter by lazy { ViewPagerAdapter(this) }
        pager.adapter = adapter
        TabLayoutMediator(tab_layout, pager) { tab, position ->
            when (position) {
                 0 -> tab.text = "option1"
                 1 -> tab.text = "option2"
                 2 -> tab.text = "option3"
            }
        }.attach()
    }
}