28
Окт
2021

TabList выдает ошибку при запуске

Вообщем выдает вот такую ошибку при запуск.Хотел осуществить переходы фрагментов переход фрагментов с помощью ViewPager2 и TabList

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.curse, PID: 8569
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.curse/com.example.curse.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager2.widget.ViewPager2.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.viewpager2.widget.ViewPager2.setAdapter(androidx.recyclerview.widget.RecyclerView$Adapter)' on a null object reference
    at com.example.curse.MainActivity.onCreate(MainActivity.java:64)
    at android.app.Activity.performCreate(Activity.java:7994)
    at android.app.Activity.performCreate(Activity.java:7978)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
    at android.os.Handler.dispatchMessage(Handler.java:106) 
    at android.os.Looper.loop(Looper.java:223) 
    at android.app.ActivityThread.main(ActivityThread.java:7656) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

Это layout в котором кодил

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.dashboard.DashboardFragment"
tools:ignore="SpeakableTextPresentCheck">


<com.google.android.material.tabs.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Блюда" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Дисерт" />

    <com.google.android.material.tabs.TabItem
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Напитки" />
</com.google.android.material.tabs.TabLayout>


<androidx.viewpager2.widget.ViewPager2
    android:id="@+id/viewPager"
    android:layout_width="394dp"
    android:layout_height="547dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    tools:layout_editor_absoluteY="50dp">


</androidx.viewpager2.widget.ViewPager2>




</androidx.constraintlayout.widget.ConstraintLayout>

так же вот адаптер

public class VPAdapter extends FragmentStateAdapter {

public VPAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
    super(fragmentManager, lifecycle);
}

@NonNull
@Override
public Fragment createFragment(int position) {


     switch (position)
     {
         case  1:
             return new blude();
         case 2:
             return new disert();
         case 3:
             return new napitok();
     }

    return new blude();
}

@Override
public int getItemCount() {
    return 3;
}
 }

И MainActivity

 TabLayout tab_layout;
ViewPager2 viewPager;
VPAdapter adapter;


tab_layout = findViewById(R.id.tab_layout);
    viewPager = findViewById(R.id.viewPager);


    FragmentManager fm = getSupportFragmentManager();
    adapter = new VPAdapter(fm,getLifecycle());
    viewPager.setAdapter(adapter);

    tab_layout.addTab(tab_layout.newTab().setText("blude"));
    tab_layout.addTab(tab_layout.newTab().setText("disert"));
    tab_layout.addTab(tab_layout.newTab().setText("napitok"));

    tab_layout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });

    viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
        @Override
        public void onPageSelected(int position) {
            tab_layout.selectTab(tab_layout.getTabAt(position));
        }
    });

Источник: https://ru.stackoverflow.com/questions/1343100/tablist-%D0%B2%D1%8B%D0%B4%D0%B0%D0%B5%D1%82-%D0%BE%D1%88%D0%B8%D0%B1%D0%BA%D1%83-%D0%BF%D1%80%D0%B8-%D0%B7%D0%B0%D0%BF%D1%83%D1%81%D0%BA%D0%B5

Тебе может это понравится...

Добавить комментарий