Привет, я хочу создать tabLayout
с помощью класса ViewPager
, чтобы я мог пронести между вкладками.NullPointerException вызывается при создании tabLayout
Я создаю вкладки I setup fragments
, viewPagerAdapter
и viewPager
. Здесь я также инициализировал tabLayout
, но я не понимаю эту ошибку. Пожалуйста, помогите мне решить.
Вот мой основной XML, который содержит табличный макет:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.davda.musicmain.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="@layout/toolbar_layout"
>
<android.support.design.widget.TabLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:id="@+id/tabLayout"
app:tabMode="scrollable"
app:tabGravity="center">
</android.support.design.widget.TabLayout
>
</include>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/viewPager"></android.support.v4.view.ViewPager>
</RelativeLayout>
Вот мой код:
tb = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(tb);
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFrags(new Albums(), "Albums");
viewPagerAdapter.addFrags(new Tracks(), "Tracks");
viewPagerAdapter.addFrags(new Artists(), "Artists");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
Вот StackTrace:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.davda.musicmain, PID: 32662
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.davda.musicmain/com.example.davda.musicmain.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2702)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2788)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1503)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6209)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.design.widget.TabLayout.setupWithViewPager(android.support.v4.view.ViewPager)' on a null object reference
at com.example.davda.musicmain.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:6745)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2655)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2788)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1503)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6209)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
D/AppTracker: App Event: crash
Application terminated.
разместит ваш XML пожалуйста –
Хорошо я добавляю XMLs – Pedo
выкладывает макет XML и метод OnCreate –