使用 ViewPager 设置 tablayout 后应用停止 运行

App stops running after setting tablayout with ViewPager

我使用来自 TabActivity 项目的经过修改的 SectionPagerAdapter 创建了自己的 TabLayout 菜单(从空的 Activity)。我只更改了 getItem() 方法并删除了页面标题方法。

public Fragment getItem(int position) {
    switch (position)
    {
        case 0:
            HomeActivity home = new HomeActivity();
            return home;
        case 1:
            PayActivity pay = new PayActivity();
            return pay;
        case 2:
            VideoActivity video = new VideoActivity();
            return video;
        case 3:
            BillActivity bill = new BillActivity();
            return bill;
        default:
            return null;
    }
}

}

而执行后

    TabLayout tabToolbar = (TabLayout) findViewById(R.id.tabToolbar);
    ViewPager viewPager = (ViewPager) findViewById(R.id.container);

    SectionsPagerAdapter mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    viewPager.setAdapter(mSectionsPagerAdapter);
    tabToolbar.setupWithViewPager(viewPager);

我的应用程序已停止。

如果我评论

tabToolbar.setupWithViewPager(viewPager);

我的菜单不起作用,但我可以在 ViewPager 中的片段之间滑动。

我想用我的菜单按钮滑动,也想用我的鼠标光标滑动。

如果您需要更多信息或资源,请告诉我。

编辑:

错误日志显示:

09-08 06:19:05.822 4207-4207/com.example.fdai3744.t_online20 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                               Process: com.example.fdai3744.t_online20, PID: 4207
                                                                               java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.fdai3744.t_online20/com.example.fdai3744.t_online20.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                   at android.os.Looper.loop(Looper.java:154)
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                                   at java.lang.reflect.Method.invoke(Native Method)
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                                Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.graphics.drawable.Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
                                                                                   at com.example.fdai3744.t_online20.MainActivity.onCreate(MainActivity.java:37)
                                                                                   at android.app.Activity.performCreate(Activity.java:6679)
                                                                                   at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                                   at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                                   at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                                                                                   at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                                                                                   at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                   at android.os.Looper.loop(Looper.java:154) 
                                                                                   at android.app.ActivityThread.main(ActivityThread.java:6119) 
                                                                                   at java.lang.reflect.Method.invoke(Native Method) 
                                                                                   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                                                                                   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

第二次编辑:

我发现我的适配器没有 "public CharSequence getPageTitle(int position)" 方法。

在 setupwithviewpager 下面添加这行代码;

viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabtoolbar));

编辑:

我已经更新了答案,你可以参考这个只有图标的选项卡的答案。