错误提示:java.lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout.

问题描述:在安卓模拟上运行程序时,一打开就报错:xx has stopped.

解决办法:根据错误提示java.lang.IllegalArgumentException: Only TabItem instances can be added to TabLayout.    

查看MainActivity.java代码中  setContentView(R.layout.activity_main);  ,直接去看activity_main.xml 的内容,发现 ViewPager 写在了 TabLayout 里。

提示中明确说到: layout内部不可以放其他东西,只能放tabItem。

改完之后的代码:

<android.support.design.widget.TabLayout
android:id="@+id/mTabLayout"
android:background="@color/colorPrimary"
app:tabGravity="fill"
app:tabIndicatorColor="@color/colorAccent"
app:tabMode="fixed"
app:tabSelectedTextColor="@color/colorAccent"
app:tabTextColor="@android:color/white"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</android.support.design.widget.TabLayout>

<android.support.v4.view.ViewPager
android:id="@+id/mViewPager"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">

</android.support.v4.view.ViewPager>

结果:已解决
posted @ 2017-07-11 07:14  Cranberries  阅读(2289)  评论(0编辑  收藏  举报