ViewPager2 must not be null in NavigationDrawer and SwipeViewTabs

半腔热情 提交于 2020-12-15 06:48:05

问题


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>

回答1:


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

code:

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


来源:https://stackoverflow.com/questions/64207278/viewpager2-must-not-be-null-in-navigationdrawer-and-swipeviewtabs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!