問題描述:在安卓模擬上運行程序時,一打開就報錯: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>
結果:已解決