app is not opening next activity after splash screen and crashing

Viewed 1199

I am making an app for a side project and with myself being pretty new to kotlin programming, I am not aware of the best solution to the problem I am facing. When I run my app, the splash screen appears but does not proceed to the login page, instead just closes the app itself. Surprisingly, it isn't giving any errors and closing the gradle by itself. Code is given below. Thanks for helping in advance.

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background"
        android:padding="32dp"
        tools:context=".MainActivity">
    
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="10dp"
            android:orientation="vertical">
    
            <EditText
                android:id="@+id/username_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="110dp"
                android:layout_marginBottom="50dp"
                android:hint="@string/username_in"/>
    
            <EditText
                android:id="@+id/password_et"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/password_in"
                android:inputType="textPassword" />
    
            <com.google.android.material.button.MaterialButton
                android:id="@+id/login_btn"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:layout_marginTop="50dp"
                android:layout_marginBottom="50dp"
                android:backgroundTint="@color/black"
                android:padding="12dp"
                android:text="@string/login_in"
                android:textSize="20sp"
                android:textAllCaps="false"
                android:textColor="@color/white"
                android:textStyle="bold"
                app:cornerRadius="10dp"/>
        </LinearLayout>
    
        <TextView
            android:id="@+id/app_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/welcome"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textColor="@color/black"
            android:textSize="40sp"
            android:textStyle="bold" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_marginTop="580dp"
            android:layout_marginBottom="580dp"
            android:text="@string/learnr"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textColor="@color/bright_yellow"
            android:textSize="40sp"
            android:textStyle="bold" />
    
    </RelativeLayout>

MainActivity.kt:

package com.example.learnr

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.EditText
import android.widget.Toast
import android.widget.Toast.makeText

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        supportActionBar?.hide()
        makeText(this, "Welcome", Toast.LENGTH_SHORT).show()

        val userName = findViewById<EditText>(R.id.username_et)
        val passWord = findViewById<EditText>(R.id.password_et)
        val loginButton = findViewById<Button>(R.id.login_btn)

        loginButton.setOnClickListener {
            val status=if (userName.text.toString() == "DevangSahani"
                && passWord.text.toString() == "devangs"
            ) "Logged in Successfully" else "Login failed, please try again"
            makeText(this, status, Toast.LENGTH_SHORT).show()
        }
    }
}

activity_splash_screen.xml:

<?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/black"
        tools:context=".SplashScreenActivity">
    
        <TextView
            android:id="@+id/splash_heading"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/learnr"
            android:textAppearance="@style/TextAppearance.AppCompat.Headline"
            android:textColor="@color/bright_yellow"
            android:textSize="60sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.497"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintVertical_bias="0.499" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

SplashScreenActivity.kt:

package com.example.learnr

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler

class SplashScreenActivity : AppCompatActivity() {

    lateinit var handler: Handler
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_splash_screen)
        supportActionBar?.hide()

        handler = Handler()
        handler.postDelayed({

            val intent = Intent(this,MainActivity::class.java)
            startActivity(intent)
            finish()

        }, 3000)

    }
}

AndroidManifest.xml:

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

    <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/Theme.Learnr">
        <activity
            android:name=".MainActivity"
            android:exported="true"
            android:theme="@style/DemoThemeMain"/>
        <activity
            android:name=".SplashScreenActivity"
            android:exported="true"
            android:theme="@style/DemoTheme">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

strings.xml:

<resources>
<string name="app_name">Learnr</string>
<string name="welcome">Welcome.</string>
<string name="learnr">Learnr.™</string>
<string name="username">Username:</string>
<string name="password">Password:</string>
<string name="username_in">Username</string>
<string name="password_in">Password</string>
<string name="login_in">Login</string>

<!-- Defined a new style with three items of color. -->
<style name="DemoTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/black</item>

    <!-- Defining that new color in ColorPrimaryDark -->
    <item name="colorPrimaryDark">@color/black</item>
    <item name="colorAccent">@color/black</item>
</style>

<!-- Defined a new style with three items of color. -->
<style name="DemoThemeMain" parent="Theme.AppCompat.Light.NoActionBar">
    <!-- Customize your theme here. -->
    <item name="colorPrimary">@color/bright_yellow</item>

    <!-- Defining that new color in ColorPrimaryDark -->
    <item name="colorPrimaryDark">@color/bright_yellow</item>
    <item name="colorAccent">@color/bright_yellow</item>
</style>

logcat:

2022-01-11 12:18:20.600 11350-11350/com.example.learnr E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.learnr, PID: 11350 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.learnr/com.example.learnr.MainActivity}: android.view.InflateException: Binary XML file line #46 in com.example.learnr:layout/activity_main: Binary XML file line #46 in com.example.learnr:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) Caused by: android.view.InflateException: Binary XML file line #46 in com.example.learnr:layout/activity_main: Binary XML file line #46 in com.example.learnr:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton Caused by: android.view.InflateException: Binary XML file line #46 in com.example.learnr:layout/activity_main: Error inflating class com.google.android.material.button.MaterialButton Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Constructor.newInstance0(Native Method) at java.lang.reflect.Constructor.newInstance(Constructor.java:343) at android.view.LayoutInflater.createView(LayoutInflater.java:852) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:1004) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:959) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1121) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082) at android.view.LayoutInflater.rInflate(LayoutInflater.java:1124) at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:1082) at android.view.LayoutInflater.inflate(LayoutInflater.java:680) at android.view.LayoutInflater.inflate(LayoutInflater.java:532) at android.view.LayoutInflater.inflate(LayoutInflater.java:479) at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:706) at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195) at com.example.learnr.MainActivity.onCreate(MainActivity.kt:13) at android.app.Activity.performCreate(Activity.java:8000) at android.app.Activity.performCreate(Activity.java:7984) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:223) at android.app.ActivityThread.main(ActivityThread.java:7656) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 2022-01-11 12:18:20.601 11350-11350/com.example.learnr E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant). at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243) at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217) at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145) at com.google.android.material.internal.ThemeEnforcement.obtainStyledAttributes(ThemeEnforcement.java:76) at com.google.android.material.button.MaterialButton.(MaterialButton.java:229) at com.google.android.material.button.MaterialButton.(MaterialButton.java:220) ... 30 more 2022-01-11 12:18:20.660 11350-11350/com.example.learnr I/Process: Sending signal. PID: 11350 SIG: 9

2 Answers

Hi please check your theme you are using. You used materialButton and theme should be material theme otherwise it will crash. If you are using correct material theme and want to hide or you do't need action bar just use NoActionBar theme in style and apply it to main.

Related