NavigationDrawer 和 SwipeViewTabs 中的 ViewPager2 不能为 null

ViewPager2 must not be null in NavigationDrawer and SwipeViewTabs

我正在尝试 link 我的带 SwipeView 选项卡的导航抽屉,问题是 logcat 告诉我我的 Viewpager 不能为 null,我已经尝试在很多地方解决这个问题方法但不能。

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()
        }
    }

片段

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

我 link 使用 ( 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>

在您的 class TabFragment 中,您正在尝试访问寻呼机视图。由于您在 onCreateView 方法中,因此合成不知道如何访问视图并在分页器上为您提供参考。 您可以在 onViewCreated 和以后的回调中执行此操作,或者您可以使用 val x 访问寻呼机。MikibeMiki

代码:

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()
    }
}