基本 Android 启动器启动画面
Basic Android launcher splash screen
我是编写代码的新手,对于我猜测的问题可能是一个非常简单的问题,我深表歉意。我关注了 Android 的 Build your first app guide. Next, I'm trying to add a launcher splash screen by following a number of tutorials (this being one of them)。当我 运行 模拟器中的应用程序加载速度如此之快以至于我无法判断启动画面是否有效。有没有办法暂时减慢模拟器的速度来检查启动画面?此外,当单击“发送”按钮时应用程序崩溃,这没有意义,因为 MainActivity 中存在 sendMessage
函数。
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.access00(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)
}
}
DisplayMessageActivity
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}
}
}
SplashActivity
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
的帮助下更新了错误
在 splash activity 中你应该使用 kotlin delay(3000) 函数。等待 3 秒。像这样:
lifecycleScope.launchWhenCreated {
delay(3000)
}
如果您想为首次发布添加教程,可以很好地解释如何使用幻灯片创建介绍:
https://www.androidhive.info/2016/05/android-build-intro-slider-app/
如果您只想在每次应用程序启动时出现一个简单的初始屏幕,请查看以下内容:
https://android.jlelse.eu/the-complete-android-splash-screen-guide-c7db82bce565
我认为您的问题是您对两个活动使用了相同的布局。为 SplashActivity 设置不同的布局。
我是编写代码的新手,对于我猜测的问题可能是一个非常简单的问题,我深表歉意。我关注了 Android 的 Build your first app guide. Next, I'm trying to add a launcher splash screen by following a number of tutorials (this being one of them)。当我 运行 模拟器中的应用程序加载速度如此之快以至于我无法判断启动画面是否有效。有没有办法暂时减慢模拟器的速度来检查启动画面?此外,当单击“发送”按钮时应用程序崩溃,这没有意义,因为 MainActivity 中存在 sendMessage
函数。
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.access00(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)
}
}
DisplayMessageActivity
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}
}
}
SplashActivity
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
的帮助下更新了错误在 splash activity 中你应该使用 kotlin delay(3000) 函数。等待 3 秒。像这样:
lifecycleScope.launchWhenCreated {
delay(3000)
}
如果您想为首次发布添加教程,可以很好地解释如何使用幻灯片创建介绍: https://www.androidhive.info/2016/05/android-build-intro-slider-app/
如果您只想在每次应用程序启动时出现一个简单的初始屏幕,请查看以下内容: https://android.jlelse.eu/the-complete-android-splash-screen-guide-c7db82bce565
我认为您的问题是您对两个活动使用了相同的布局。为 SplashActivity 设置不同的布局。