简体   繁体   English

android.support.v4.view.ViewPager无法转换为android.support.design.widget.TabLayout

[英]android.support.v4.view.ViewPager cannot be cast to android.support.design.widget.TabLayout

When i connect Tablayout and view pager it causes my app to crash i'm using the latest version of the android support library. 当我连接Tablayout并查看寻呼机时,它导致我的应用程序崩溃,我使用的是最新版本的android支持库。 How else would i go about making an app with sliding tabs. 我还要如何制作带有滑动标签的应用程序。

 ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter pagerAdapter =
                new PagerAdapter(getSupportFragmentManager(),MainActivity.this);
        viewPager.setAdapter(pagerAdapter);


        TabLayout tabLayout = (TabLayout) findViewById(R.id.pager);

It happens because you are using the same id to find the ViewPager and the TabLayout. 发生这种情况是因为您使用相同的id来查找ViewPagerTabLayout.

   ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
   TabLayout tabLayout = (TabLayout) findViewById(R.id.pager);

In your layout check the right id for the TabLayout. 在您的布局中,检查TabLayout的正确ID。

I fixed the layout by changing the width of CoordinatorLayout to wrap_content and changing the width and height of view pager to wrap_content. 我通过将CoordinatorLayout的宽度更改为wrap_content并将视图分页器的宽度和高度更改为wrap_content来修复了布局。 I also gave TabLayout its own id. 我还给了TabLayout自己的ID。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TabLayout 中的 setupWithViewPager(android.support.v4.view.ViewPager) 无法应用于 (androidx.viewpager..widget.ViewPager) - setupWithViewPager(android.support.v4.view.ViewPager) in TabLayout cannot br applied to (androidx.viewpager..widget.ViewPager) android.support.v4.view.ViewPager无法转换为android.support.v4.view.ViewPager - android.support.v4.view.ViewPager cannot be cast to android.support.v4.view.ViewPager Android.widget.framelayout无法强制转换为android.support.v4.view.viewpager - Android.widget.framelayout cannot be cast to android.support.v4.view.viewpager android.support.design.widget.TabLayout 与 ViewPager 无法正常工作 - android.support.design.widget.TabLayout not working properly with ViewPager ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.support.v4.view.ViewPager $ LayoutParams - ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.view.ViewPager$LayoutParams android.support.design.widget.TabLayout - 无法解析符号“设计” - android.support.design.widget.TabLayout - cannot resolve symbol 'design' android.support.v4.view.ViewPager $ LayoutParams无法转换为android.view.ViewGroup $ MarginLayoutParams - android.support.v4.view.ViewPager$LayoutParams cannot be cast to android.view.ViewGroup$MarginLayoutParams ClassNotFoundException:android.support.v4.View.ViewPager - ClassNotFoundException: android.support.v4.View.ViewPager 无法将null强制转换为非null类型android.support.v4.view.ViewPager-KOTLIN - null cannot be cast to non-null type android.support.v4.view.ViewPager - KOTLIN android.support.v4.view.ViewPager无法强制转换为com.package.CustomViewPager - android.support.v4.view.ViewPager cannot be cast to com.package.CustomViewPager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM