Android開發The style on this component requires your app theme to be Theme.AppCompat (or a descendant)的解決方法


問題:

Caused by: android.view.InflateException: Binary XML file line #100 in xxx_layout: Binary XML file line #100 in xxx_layout: Error inflating class com.google.android.material.XXX
Caused by: android.view.InflateException: Binary XML file line #100 in xxx_layout: Error inflating class com.google.android.material.XXX
Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        ...
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.AppCompat (or a descendant).

原因:

xml布局中使用了Material控件,所以要求Application或者該Activity的android:theme樣式必須是Theme.AppCompa或者其子樣式。
那么問題來了?就必須將android:theme樣式改為Theme.AppCompa或者其子樣式嗎?怎么區分是不是Theme.AppCompa樣式呢?
其實使用了Material控件,只需要Theme.AppCompa或者其子樣式包含colorPrimary、colorPrimaryDark、colorAccent這三個名稱的顏色即可。

解決方法

<style name="MyTheme" parent="@android:style/Theme.Material.Light.NoActionBar">
	...
	<!--添加下面三個名稱的顏色(顏色值隨便),MyTheme這個樣式滿足Theme.AppCompa,就可以解決上面問題-->
	<item name="colorPrimary">#ffff00</item>
	<item name="colorPrimaryDark">#ff00ff</item>
	<item name="colorAccent">#ff0000</item>
</style>


免責聲明!

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



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