錯誤提示: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>

結果:已解決


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM