Android 不同主题的PNG会导致充气异常
我有一个应用程序有两个不同的颜色主题(一个深色和一个浅色主题)。我的应用程序的一部分,我使用png作为小部件的背景,这两个主题应该有不同的颜色 styles.xml:Android 不同主题的PNG会导致充气异常,android,Android,我有一个应用程序有两个不同的颜色主题(一个深色和一个浅色主题)。我的应用程序的一部分,我使用png作为小部件的背景,这两个主题应该有不同的颜色 styles.xml: <resources> <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar"> ... </style> <style name="AppTheme.DARK" pare
<resources>
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
...
</style>
<style name="AppTheme.DARK" parent="AppTheme">
<item name="my_png">@drawable/my_png_theme_dark</item>
<item name="my_png2">@drawable/my_png2_theme_dark</item>
</style>
<style name="AppTheme.DARK" parent="AppTheme">
<item name="my_png">@drawable/my_png_theme_light</item>
<item name="my_png2">@drawable/my_png2_theme_light</item>
</style>
</resources>
我对不同颜色的矢量绘图也做了同样的处理,效果非常好。我不明白为什么PNG不起作用
更新
我刚刚意识到,并非所有设备都会出现这种情况。我在我的华为P9 lite API 24(真正的硬件)上试用过,它崩溃了,但在10.1 WXGA API 22(AVD)上它可以工作。我自己找到了解决方案。我不知道到底是什么导致了异常,但这是有效的:
attrs.xml
:
<resources>
<attr name="my_background" format="reference"/>
</resources>
<resources>
<style name="AppTheme.LIGHT" parent="AppTheme">
<item name="checkbox_background">@drawable/my_background_theme_light</item>
</style>
<style name="AppTheme.DARK" parent="AppTheme">
<item name="checkbox_background">@drawable/my_background_theme_dark</item>
</style>
</resources>
可绘制/我的背景主题灯
:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/my_png1_theme_light" android:state_checked="true"/>
<item android:drawable="@drawable/png2_theme_light" android:state_checked="false"/>
</selector>
你能把你的
my_background.xml
贴在这里吗?它就在attrs.xml
你的活动使用什么主题?要检查问题是否与主题相关,您可以将android:theme=“@style/AppTheme.DARK”添加到my_activity.xml的复选框中。我在清单的应用程序标记中有android:theme=“@style/AppTheme.DARK
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myapp/com.myapp.myapp.ui.activities.MyActivity}: android.view.InflateException: Binary XML file line #76: Binary XML file line #76: Error inflating class CheckBox
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6523)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)
Caused by: android.view.InflateException: Binary XML file line #76: Binary XML file line #76: Error inflating class CheckBox
Caused by: android.view.InflateException: Binary XML file line #76: Error inflating class CheckBox
Caused by: android.content.res.Resources$NotFoundException: Drawable com.myapp.myapp:drawable/my_background with resource ID #0x7f080099
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/my_background.xml from drawable resource ID #0x7f080099
<resources>
<attr name="my_background" format="reference"/>
</resources>
<resources>
<style name="AppTheme.LIGHT" parent="AppTheme">
<item name="checkbox_background">@drawable/my_background_theme_light</item>
</style>
<style name="AppTheme.DARK" parent="AppTheme">
<item name="checkbox_background">@drawable/my_background_theme_dark</item>
</style>
</resources>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/my_png1_theme_light" android:state_checked="true"/>
<item android:drawable="@drawable/png2_theme_light" android:state_checked="false"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/my_png1_theme_dark" android:state_checked="true"/>
<item android:drawable="@drawable/png2_theme_dark" android:state_checked="false"/>
</selector>