ViewPager2 non deve essere nullo in NavigationDrawer e SwipeViewTabs

Oct 05 2020

Sto cercando di collegare il mio cassetto di navigazione con le schede SwipeView, il problema è che il logcat mi dice che il mio Viewpager non deve essere nullo, ho provato a risolvere questo problema in molti modi ma non ci sono riuscito.

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

Frammento

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

contenere_main.xml

Ho collegato questo file con (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>

Risposte

JorgeLeonardo Oct 06 2020 at 11:12

Nella tua classe TabFragment stai tentando di accedere alla visualizzazione del pager. Dato che sei nel metodo onCreateView, il sintetico non sa come accedere a view e fornirti riferimenti sul pager. Puoi farlo in onViewCreated e nei callback successivi, oppure puoi usare val x per accedere al cercapersone. MikibeMiki

codice:

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