Home > other >  app is not opening next activity after splash screen and crashing
app is not opening next activity after splash screen and crashing

Time:01-10

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

CodePudding user response:

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.

CodePudding user response:

I guess your app is crashing on supportActionBar?.hide(). If you want to remove the action bar, you can edit the DemoThemeMain theme and replace DarkActionBar with NoActionBar which will present in the parent tag of the style.

  •  Tags:  
  • Related