I am building my first app and i am trying to change activities using buttons.The first button works fine. But the second crashes the app giving this error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference I tried researching, but i didn't find a working solution. So please help me out here
login.kt
package com.example.login
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class login : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_login)
val loginbutton = findViewById<Button>(R.id.signinbtn)
loginbutton.setOnClickListener {
val intent = Intent(this,gamefeed::class.java)
startActivity(intent)
finish()
}
val signupbutton = findViewById<Button>(R.id.signupbtn)
signupbutton.setOnClickListener {
val intent = Intent(this,Registration::class.java)
startActivity(intent)
finish()
}
}
}
activity_login.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:id="@ id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="@drawable/ic_spalsh"
tools:ignore="InvalidId">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/constraintLayout"
android:layout_width="392dp"
android:layout_height="217dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<ImageView
android:id="@ id/imageView2"
android:layout_width="399dp"
android:layout_height="228dp"
android:layout_marginBottom="32dp"
android:contentDescription="@string/todo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.285"
app:srcCompat="@drawable/login1" />
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@ id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="@string/Join_with_us"
android:textColor="@color/black"
android:textSize="34sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/constraintLayout"
tools:ignore="TextContrastCheck" />
<LinearLayout
android:id="@ id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="30dp"
android:layout_marginTop="30dp"
android:layout_marginEnd="30dp"
android:background="@drawable/background_edittext"
android:elevation="8dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView3">
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:ems="10"
android:hint="@string/username"
android:importantForAutofill="no"
android:inputType="textPersonName|textEmailAddress"
android:padding="24dp"
tools:ignore="TextContrastCheck"
/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF"
/>
<com.google.android.material.textfield.TextInputEditText
android:id="@ id/editTextTextPassword"
android:layout_width="match_parent"
android:layout_height="71dp"
android:ems="10"
android:hint="@string/password"
android:importantForAutofill="no"
android:inputType="textPassword"
android:padding="24dp" />
</LinearLayout>
<TextView
android:id="@ id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="@string/forgot_password"
android:textColor="@color/purple_700"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout"
tools:ignore="TextContrastCheck" />
<Button
android:id="@ id/signupbtn"
style="@style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="100dp"
android:backgroundTint="@color/white"
android:text="@string/Dont_have_account"
android:textColor="@color/black"
android:textSize="14sp"
android:visibility="visible"
app:cornerRadius="50dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/signinbtn"
tools:visibility="visible" />
<Button
android:id="@ id/signinbtn"
android:layout_width="176dp"
android:layout_height="68dp"
android:layout_marginTop="24dp"
android:background="@drawable/background_btn"
android:backgroundTint="@color/bottom_nav_background"
android:padding="15dp"
android:text="@string/login"
android:textColor="@color/white"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView4"
app:layout_constraintWidth_percent="0.6"
tools:ignore="DuplicateSpeakableTextCheck" />
</androidx.constraintlayout.widget.ConstraintLayout>
Registration.kt
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
class Registration : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
val signinbutton2 = findViewById<Button>(R.id.loginbtn)
signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
}
activity_registration.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:id="@ id/activity_registration"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Registration"
android:background="@drawable/ic_spalsh">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@ id/constraintLayout3"
android:layout_width="393dp"
android:layout_height="213dp"
android:background="@drawable/registration1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHeight_percent="0.40"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</androidx.constraintlayout.widget.ConstraintLayout>
<TextView
android:id="@ id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="@string/Register_Here"
android:textColor="@color/black"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/constraintLayout3"
tools:ignore="TextContrastCheck" />
<LinearLayout
android:id="@ id/linearLayout2"
android:layout_width="356dp"
android:layout_height="354dp"
android:layout_marginStart="30dp"
android:layout_marginTop="12dp"
android:layout_marginEnd="30dp"
android:background="@drawable/background_edittext"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.263"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView6">
<EditText
android:id="@ id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:ems="10"
android:hint="@string/email"
android:importantForAutofill="no"
android:inputType="textPersonName|textEmailAddress"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="@ id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:ems="10"
android:hint="@string/username"
android:inputType="textPersonName"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck,TextContrastCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="@ id/age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@null"
android:ems="10"
android:hint="@string/age"
android:inputType="textPersonName"
android:padding="10dp"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<TextView
android:id="@ id/gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="@null"
android:text="@string/gender"
android:textColor="#AEA8BC"
android:textColorHighlight="@color/purple_700"
android:textSize="20sp"
tools:ignore="TextContrastCheck" />
<RadioGroup
android:layout_width="match_parent"
android:layout_height="44dp"
android:orientation="horizontal">
<RadioButton
android:id="@ id/malerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="@string/male"
tools:ignore="TouchTargetSizeCheck" />
<RadioButton
android:id="@ id/femalerb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:hint="@string/female"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck" />
<RadioButton
android:id="@ id/otherrb"
android:layout_width="128dp"
android:layout_height="wrap_content"
android:layout_marginStart="40dp"
android:layout_weight="1"
android:hint="@string/other"
tools:ignore="TouchTargetSizeCheck" />
</RadioGroup>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="@ id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/password1"
android:inputType="textPassword"
android:padding="10dp"
tools:ignore="TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<EditText
android:id="@ id/confirmpassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="@string/confirm_password"
android:inputType="textPassword"
android:padding="10dp"
tools:ignore="TextContrastCheck,TouchTargetSizeCheck"
android:importantForAutofill="no" />
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#EEEEEF" />
<Button
android:id="@ id/regbtn"
android:layout_width="131dp"
android:layout_height="wrap_content"
android:layout_marginStart="105dp"
android:background="@drawable/background_btn"
android:textColor="@color/white"
android:text="@string/register"
tools:ignore="DuplicateSpeakableTextCheck" />
</LinearLayout>
<Button
android:id="@ id/signin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="123dp"
android:background="#00FFFFFF"
android:text="@string/Allready_have_account"
android:textColor="@color/black"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/linearLayout2"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
Error
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.login/com.example.login.Registration}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
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: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.login.Registration.onCreate(Registration.kt:15)
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)
CodePudding user response:
You get an error because there is no loginbtn on the activity_registration.xml page
class Registration : AppCompatActivity() {
private lateinit var regbtn:Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_registration)
regbtn=findViewById<Button>(R.id.regbtn)
signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
I recommend you to look at the binding structure
build.gradle(module)
buildFeatures {
viewBinding true
dataBinding true
}
Registration.kt
class Registration : AppCompatActivity() {
private lateinit var binding: RegistrationBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityAdminBinding.inflate(layoutInflater)
val view=binding.root
setContentView(view)
binding.signinbutton2.setOnClickListener {
val intent = Intent(this,login::class.java)
startActivity(intent)
finish()
}
}
}