• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: Android Studio emulator starts, but flashes white screen and crashes the app

Resolved: Android Studio emulator starts, but flashes white screen and crashes the app

0
By Isaac Tonny on 16/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

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 of what is happening
Animation of what is going on.

Answer:

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)

If you have better answer, please add a comment about this, thank you!

android java kotlin
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: linq2db throws exception when filtering by nested collection

02/04/2023

Resolved: Table data is coming as empty in React

02/04/2023

Resolved: In a Pinescript v5 Strategy why is my bool not working?

02/04/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.