简体   繁体   English

Android Studio 模拟器启动,但闪烁白屏并使应用程序崩溃

[英]Android Studio emulator starts, but flashes white screen and crashes the app

As the title suggests, when I start up the emulator, it starts up totally fine, but then immediately shows a white screen, followed by the home screen.正如标题所暗示的,当我启动模拟器时,它启动完全正常,但随后立即显示白屏,然后是主屏幕。 No app appears to have been installed.似乎没有安装任何应用程序。 A few things to note:需要注意的几点:

  • I am using the Pixel 5 API 32 emulator, and I checked in my gradle settings that the target is API 32.我正在使用 Pixel 5 API 32 模拟器,并在我的 gradle 设置中检查了目标是 API 32。
  • Attached below is my AndroidManifest.xml and MainActivity.kt code.下面附上我的 AndroidManifest.xml 和 MainActivity.kt 代码。 I am very new to Kotlin programming, so it is very possible I may have missed something.我对 Kotlin 编程很陌生,所以我很可能错过了一些东西。 Please let me know if you need any other code.如果您需要任何其他代码,请告诉我。
  • I am running this code on an M1 Macbook Air 2020. I have the latest version of Android Studio.我在 M1 Macbook Air 2020 上运行此代码。我有最新版本的 Android Studio。
  • Absolutely no error appears (which is frustrating...)绝对不会出现错误(令人沮丧...)

Thank you in advance for your help.预先感谢您的帮助。

AndroidManifest.xml AndroidManifest.xml

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

    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GroupUpAndroid"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

MainActivity.kt MainActivity.kt

package com.example.groupupandroid

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import com.example.groupupandroid.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding


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

        binding.registerButton.setOnClickListener {
            binding.registerFields.visibility = View.VISIBLE
            binding.loginFields.visibility = View.GONE
        }

        binding.loginButton.setOnClickListener {
            binding.registerFields.visibility = View.GONE
            binding.loginFields.visibility = View.VISIBLE
        }


    }
}

正在发生的事情的 GIF

Animation of what is going on.正在发生的事情的动画。

If you take a look at your mainActivity.kt code, you will know that this is not an emulator problem ;)如果你看看你的 mainActivity.kt 代码,你就会知道这不是模拟器问题;)

here you are declaring a lateinit variable of type 'ActivityMainBinding' It is not initialized anywhere later in any way!在这里,您声明了一个 'ActivityMainBinding' 类型的 lateinit 变量它不会以任何方式在以后的任何地方初始化!

 private lateinit var binding: ActivityMainBinding

you will definitevely find a solution here and I encourage you to read more about viewBinding :)您肯定会在这里找到解决方案,我鼓励您阅读有关 viewBinding 的更多信息 :)

but if you do not want to do that:但如果你不想这样做:

private lateinit var binding: ActivityMainBinding

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    // as you can see here it is initalized
    binding = ActivityMainBinding.inflate(layoutInflater)
    val view = binding.root

    // and because it is initalized, you can now reference it without the app crashing
    binding.registerButton.setOnClickListener {
            binding.registerFields.visibility = View.VISIBLE
            binding.loginFields.visibility = View.GONE
    }

    setContentView(view)
}

edit / tip: The app crash will not always generate the Java stack trace, but if you run the app for another 3-5 times or run it in the debug mode it probably will generate it at some point.编辑/提示:应用程序崩溃并不总是会生成 Java 堆栈跟踪,但如果您再运行应用程序 3-5 次或在调试模式下运行它,它可能会在某个时候生成它。 (worked for me so far) (到目前为止为我工作)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM