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>