![](/img/trans.png)
[英]ActionBarSherlock & ShareActionProvider cause InflateException
[英]PNGs for different Themes cause InflateException
我有一个具有两种不同颜色主题(深色和浅色主题)的应用程序。 我是我的应用程序的一部分,我使用 png 作为小部件的背景,它在两个主题中应该有不同的 colors。
styles.xml:
<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>
attrs.xml:
<resources>
<attr name="my_png" format="reference"/>
<attr name="my_png2" format="reference"/>
</resources>
可绘制/my_background.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="?attr/my_png" android:state_checked="true"/>
<item android:drawable="?attr/my_png2" android:state_checked="false"/>
</selector>
布局/my_activity.xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:id="@+id/my_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="@drawable/my_background"
android:background="@android:color/transparent"/>
</LinearLayout>
我在values-mdpi
、 values-hdpi
、 values-xhdpi
和values-xxhdpi
中提供了这个 png。
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
我对不同 colors 中的矢量可绘制对象执行相同操作,效果很好。 我不知道为什么它不适用于 PNG。
更新我刚刚意识到这不会发生在每台设备上。 我已经在我的华为 P9 lite API 24(真正的硬件)上尝试过它并且它崩溃了,但是在 10.1 WXGA API 22(AVD)上它可以工作。
我自己找到了解决方案。 我不知道究竟是什么导致了异常,但这有效:
attrs.xml
:
<resources>
<attr name="my_background" format="reference"/>
</resources>
styles.xml
:
<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>
drawable/my_background_theme_light
:
<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>
drawable/my_background_theme_dark
:
<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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.