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

时间:2017-09-07 20:14:58

标签: android android-fragments android-viewpager android-tablayout

我使用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)”方法。

1 个答案:

答案 0 :(得分:1)

在setupwithviewpager;

下面添加以下代码
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabtoolbar));

编辑:

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

https://stackoverflow.com/a/32173670/5339829