简体   繁体   中英

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.
  • Attached below is my AndroidManifest.xml and MainActivity.kt code. I am very new to Kotlin programming, so it is very possible I may have missed something. 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.
  • Absolutely no error appears (which is frustrating...)

Thank you in advance for your help.

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

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 ;)

here you are declaring a lateinit variable of type 'ActivityMainBinding' It is not initialized anywhere later in any way!

 private lateinit var binding: ActivityMainBinding

you will definitevely find a solution here and I encourage you to read more about 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. (worked for me so far)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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