安卓为什么我在夸大初始屏幕的图层列表时出错



我正在尝试为我的Android应用程序创建一个启动画面,我对Android不太熟悉。尝试运行我的应用程序时,出现以下错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.example/com.Example.example.SplashActivity}: android.view.InflateException: Binary XML file line #2 in com.example.example:layout/activity_splash: Binary XML file line #2 in com.example.example:layout/activity_splash: Error inflating class layer-list

我尝试在网上研究这个,但我还没有看到类似的场景。

activity_splash.xml:

<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:drawable="@color/colorBlack"/>
<!-- Set the Background Image-->
<item android:gravity="center" android:width="500dp" android:height="700dp">
<bitmap
android:gravity="fill_horizontal|fill_vertical"
android:src="@drawable/logo"/>
</item>

</layer-list>

我在 AndroidManifest 中添加了以下行.xml:

<activity
android:name=".SplashActivity"
android:theme="@style/SplashScreenTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

欢迎任何反馈。我想知道我缺少什么,或者是否有有关如何创建初始屏幕的文档,因为我找不到任何文档。

一种常见的方法是:

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

在您的activity_splash.xml

并将以下样式添加到样式中.xml

<style name="SplashScreen" parent="Theme.AppCompat.Light.NoActionBar">
<item name="android:windowBackground">@drawable/activity_splash</item>
</style>

在清单中:

<activity android:name=".activities.Splash" android:theme="@style/SplashScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

在这种情况下,启动活动没有 xml 布局:

package com.something.iloveher.activities
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
class Splash : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}

最新更新