-1

I am developing a simple login and register android app and everything have been coded and no errors were issued. The app can be ran successfully and all, but, the screen doesn't show the launcher activity. Like, it only shows up and then immediately close the app. I hope someone can catch what I am trying to say here. Also, I'm 100% sure that i have registered the activities on the manifest file.

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>

<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/AppTheme"
        tools:ignore="GoogleAppIndexingWarning">

    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"/>
            <category android:name="android.intent.category.LAUNCHER"/>
            <category android:name="android.intent.category.DEFAULT"/>
        </intent-filter>
    </activity>

    <activity
            android:name=".LoginActivity">
    </activity>
    <activity
            android:name=".RegisterActivity">
    </activity>


    <service android:name=".MyFirebaseMessagingService">
        <intent-filter>
            <action android:name="com.google.firebase.MESSAGING_EVENT"/>
        </intent-filter>
    </service>
    <service android:name=".MyFirebaseInstanceIDService">
        <intent-filter>
            <action android:name="com.google.firebase.INSTANCE_ID_EVENT"/>
        </intent-filter>
    </service>

    <meta-data
            android:name="android.app.alertbox"
            android:resource="@xml/automotive_app_desc"/>
</application>

</manifest> 

And this is what i get from the error log.

Caused by: kotlin.KotlinNullPointerException
        at com.example.acer.alertbox.MainActivity.onCreate(MainActivity.kt:17)
        at android.app.Activity.performCreate(Activity.java:6910)
        at 
   android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
        at 
   android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
        at 
   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
        at android.app.ActivityThread.-wrap12(ActivityThread.java) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
        at android.os.Handler.dispatchMessage(Handler.java:105) 
        at android.os.Looper.loop(Looper.java:156) 
        at android.app.ActivityThread.main(ActivityThread.java:6523) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at 


 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831) 

This is my MainActivity.kt code:

package com.example.acer.alertbox

import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button

class MainActivity : AppCompatActivity() {

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

        val btnLogin: Button? = null
        val btnRegister: Button? = null

        btnLogin!!.setOnClickListener {
            val intent = Intent(this@MainActivity, LoginActivity::class.java)
            startActivity(intent)
        }

        btnRegister!!.setOnClickListener {
            val regIntent = Intent(this@MainActivity, RegisterActivity::class.java)
            startActivity(regIntent)
        }

    }
}

*Update: I have solved the issues by removing the null pointers. I did not know that there was any error because I did not check nor did I know how to open the Log Error so that is why I did not know this type of question has been asked before. I'm a new android developer by the way.

2 Answers2

0

Do you Add this ⬇ in your Activity tag within manifest file.

            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>

This code is use for first launcher activity specification.

Prince Dholakiya
  • 3,017
  • 23
  • 38
0

So After a long discussion i can see that you are setting null to your button

val btnLogin: Button? = null
val btnRegister: Button? = null

and then on null you are setting onClickListener that's why it's crashing so you have to set the reference of button something like this after your declaration

btnLogin = findViewById(R.id.buttonid) as Button
btnRegister = findViewById(R.id.buttonid) as Button

the button id you can find from your activity_main layout file.

MadLeo
  • 398
  • 1
  • 4
  • 19