样式崩溃的Android启动画面java.lang.Exception:drawable中的递归引用

时间:2018-08-28 10:08:54

标签: java android xml styles

具有样式的启动画面设计崩溃。该错误仅发生在Android API级别27以上。代码结构如下,

style.xml

<style name="SplashScreenTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name ="android:windowBackground"> @drawable/splash_screen_background</item>
</style>

splash_screen_background.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
    android:opacity="opaque">
    <item
        android:drawable="@color/colorPrimary"/>

    <item>
        <bitmap
            android:gravity="center"
            android:src="@mipmap/ic_launcher"/>
    </item>
</layer-list>

Manifest.xml

<application
    android:...
    android:theme="@style/AppTheme">

例外:

Exception java.lang.RuntimeException: Unable to start activity ComponentInfo{com....Views.SplashActivity}: android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2778)
android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2856)
android.app.ActivityThread.-wrap11 ()
android.app.ActivityThread$H.handleMessage (ActivityThread.java:1589)
android.os.Handler.dispatchMessage (Handler.java:106)
android.os.Looper.loop (Looper.java:164)
android.app.ActivityThread.main (ActivityThread.java:6494)
java.lang.reflect.Method.invoke (Method.java)
com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:438)
com.android.internal.os.ZygoteInit.main (ZygoteInit.java:807)
arrow_drop_down
Caused by android.view.InflateException: Binary XML file line #8: Binary XML file line #8: Error inflating class ImageView
arrow_drop_down
Caused by android.view.InflateException: Binary XML file line #8: Error inflating class ImageView
arrow_drop_down
Caused by android.content.res.Resources$NotFoundException: Drawable com...:mipmap/ic_launcher with resource ID #0x7f0e0000
arrow_drop_down
Caused by android.content.res.Resources$NotFoundException: File res/mipmap-anydpi-v26/ic_launcher.xml from drawable resource ID #0x7f0e0000
arrow_drop_down
Caused by android.content.res.Resources$NotFoundException: Drawable com...:mipmap/ic_launcher with resource ID #0x7f0e0000
arrow_drop_down
Caused by android.content.res.Resources$NotFoundException: File res/mipmap-anydpi-v26/ic_launcher.xml from drawable resource ID #0x7f0e0000
arrow_drop_down
Caused by java.lang.Exception: Recursive reference in drawable

1 个答案:

答案 0 :(得分:0)

要将主题应用到活动中,您应该这样做。

让我们说我创建了这样的Splash主题

<style name="SplashTheme" parent="Theme.MaterialComponents.NoActionBar">
    <item name="android:windowBackground">
        @drawable/splash
    </item>
</style>

我已经在drawable中创建了splash.xml

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@android:color/white" />
<item>
    <bitmap
        android:gravity="center"
        android:src="@drawable/web_hi_res_512" />
</item>
</layer-list>

然后您可以在清单中使用它

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <activity
        android:name=".SplashActivity"
        android:screenOrientation="portrait"
        android:theme="@style/SplashTheme"> // I have applied the theme to this activity
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

 ....
</application>