基本的Android启动器闪屏界面

4

我按照Android的构建您的第一个应用程序指南进行操作。接下来,我尝试通过遵循多个教程(其中之一)添加启动器闪屏。当我在模拟器中运行应用程序时,它加载得非常快,以至于我无法确定闪屏是否起作用。有没有办法暂时减慢模拟器的速度以检查闪屏?此外,当单击“发送”按钮时,应用程序会崩溃,这是没有道理的,因为sendMessage函数存在于MainActivity中。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.myfirstapp, PID: 9242
    java.lang.IllegalStateException: Could not find method sendMessage(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton with id 'button'
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:436)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:393)
        at android.view.View.performClick(View.java:7125)
        at android.view.View.performClickInternal(View.java:7102)
        at android.view.View.access$3500(View.java:801)
        at android.view.View$PerformClick.run(View.java:27336)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)

我真的不确定出了什么问题,或者这个应用程序是否太小,无法显示启动画面。我也不确定在这里分享我的项目的最佳方式。因此,以下是所有代码。

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.myfirstapp">

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

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

        <activity android:name=".DisplayMessageActivity"
                  android:parentActivityName=".MainActivity">
            <!-- For API 15 -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.kt

package com.example.myfirstapp

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.EditText

const val EXTRA_MESSAGE = "come.example.myfirstapp.MESSAGE"

class MainActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }

    //Called when user taps Send button
    fun sendMessage(view: View)
    {
        val editText = findViewById<EditText>(R.id.editTextTextPersonName)
        val message = editText.text.toString()
        val intent = Intent(this, DisplayMessageActivity::class.java).apply {putExtra(EXTRA_MESSAGE, message)}
        startActivity(intent)
    }
}

显示消息活动

package com.example.myfirstapp

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView

class DisplayMessageActivity : AppCompatActivity()
{
    override fun onCreate(savedInstanceState: Bundle?)
    {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_display_message)

        //Get the Intent that started this activity and extract the string
        val message = intent.getStringExtra(EXTRA_MESSAGE)

        //Capture the layout's TextView and set the string as its text
        val text = findViewById<TextView>(R.id.textView).apply {text = message}
    }
}

启动画面活动

package com.example.myfirstapp

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity

class SplashActivity : AppCompatActivity()
{
    override fun onCreate(savedIntanceState: Bundle?)
    {
        setTheme(R.style.AppTheme)
        super.onCreate(savedIntanceState)
        setContentView(R.layout.activity_main)
    }
}

可绘制的splash_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>

值 styles.xml

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

    <!-- Splash Screen theme. -->
    <style name="SplashTheme" parent="Theme.AppCompat.NoActionBar">
        <item name="android:windowBackground">@drawable/splash_background</item>
    </style>
</resources>

编辑 - 在S T的帮助下更新了错误


请查看此链接:https://dev59.com/AloT5IYBdhLWcg3wnQn9#38367699 - S T
我安装了API v29版本。v30版本没有提供。现在错误已经消失了,但是在它加载得如此之快之前,我不知道启动画面是否真正起作用。我的代码有任何错误吗?有没有一种方法可以减慢模拟器的速度以查看启动画面? - Tom
3个回答

3

感谢@sixpain提供的这两个链接。我想第二个链接中的教程(选项1或2)是我要找的。在选项2中,我不明白SplashActivity.kt的第11行中UserDb来自哪里。你知道它是在哪里声明的或者为什么需要吗?我的应用程序非常简单,启动速度非常快,以至于我无法确定它是否实际工作。在我弄清楚这个启动加载器屏幕之后,我也对你提供的第一个链接感兴趣。 - Tom
关于你第二个链接中的选项2,还有一个问题: “在此步骤中,您只需要创建一个新的启动 Activity 并在 AndroidManifest.xml 文件中将其分配为启动器主题” 对我来说没有意义。 我创建了一个新的空活动,但不确定如何在清单中正确引用主题。 选项1要求更新 android:theme="@style/AppTheme.Launcher",但是我不知道如何在选项2中对新活动执行相同的操作。 谢谢。 - Tom
我的错,我链接了一个高级教程。特别是UserDB是该示例中使用的应用程序的数据库,因此您不需要为基本应用程序使用它。尝试查看这个链接,很容易:https://android.jlelse.eu/launch-screen-in-android-the-right-way-aca7e8c31f52。 - sixpain
关于你的第二个问题,在清单文件中当你声明一个活动时,你可以为每个活动设置属性android:theme="<your style>"。我为你提供了我的应用程序示例链接: https://github.com/MicheleSalvatori/FiscalCode/blob/master/app/src/main/AndroidManifest.xml 在那里,你可以看到在我的闪屏活动中清单的声明。在这里,你可以设置xml属性,特别是查看android:theme,它被设置为我定义的样式。你只需在<activity>下添加属性android:theme,就可以为每个活动进行设置。 - sixpain
感谢@sixpain。这个基础教程也不起作用。使用教程中的完全相同的代码,清单中未定义“.activities.MainActivity”。我以为应该是“.MainActivity”而不是“.activities.MainActivity”。尽管它能够编译,但应用程序立即崩溃,并出现错误“java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.splashscreen/com.example.splashscreen.MainActivity}: android.content.res.Resources$NotFoundException: Drawable com.example.splashscreen:drawable/splash with resource ID #0x7f06006d”。看起来所有的教程都已经过时了,不能再起作用了。 - Tom
不,我用这个来设计我的启动屏幕并且是正确的。错误是由于找不到drawable引起的。在你的应用程序中,你没有drawable“splash”,但你正在试图引用它。首先,你需要为启动屏幕创建一个drawable,所以在drawable文件夹下创建一个名为“splash.xml”的新XML文件,并将此内容粘贴进去 https://gist.githubusercontent.com/pallocchi/35b82e753ef9506965d0b7705e80b57a/raw/09d49df7e02005b5739958eddb954d0665e0b533/splash.xml另外,“。activities.MainActivity”是因为他有一个包含MainActivity的“activities”文件夹。 - sixpain

0
我认为你的问题在于你正在为两个活动使用相同的布局。为SplashActivity设置不同的布局。

-1
在启动画面活动中,您应该使用Kotlin的delay(3000)函数。它会等待3秒钟,就像这样:
        lifecycleScope.launchWhenCreated { 
        delay(3000)
    }

谢谢您的建议。lifecycleScope未被识别并提示创建抽象属性。我认为这可能比我现在所处的位置要高一些。如果这个延迟能够起作用,我不确定我是否已经正确编写了其余的代码,因为在单击发送按钮时会出现错误。 - Tom
将以下内容添加到Gradle中:implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.2.0-alpha01" - Nurseyit Tursunkulov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接