在NavigationDrawer和SwipeViewTabs中,ViewPager2不得为null [英] ViewPager2 must not be null in NavigationDrawer and SwipeViewTabs

查看:89
本文介绍了在NavigationDrawer和SwipeViewTabs中,ViewPager2不得为null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将导航抽屉与SwipeView选项卡链接,问题是logcat告诉我Viewpager不能为null,我试图通过多种方式解决此问题,但不能解决.

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

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

contain_main.xml

我将此文件与(类TabFragment:Fragment())链接

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>

推荐答案

在您的类TabFragment中,您尝试访问页面视图.由于您使用的是onCreateView方法,因此综合程序不知道如何访问视图并为您提供有关分页器的参考. 您可以在onViewCreated和更高版本的回调中执行此操作,也可以使用val x访问分页器.

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

这篇关于在NavigationDrawer和SwipeViewTabs中,ViewPager2不得为null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆