Why is my app just crashing?
It is supposed to be a training app that gives you instructions via multiple pages.
But it is throwing this error in the console when i run it on my phone and on my virtual phone.
These are the files that are in my project.
Please help.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.hectorwithc.training_handbook, PID: 7400
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.hectorwithc.training_handbook/com.hectorwithc.training_handbook.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3365)
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 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference
at android.content.ContextWrapper.getApplicationInfo(ContextWrapper.java:173)
at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174)
at android.content.Context.obtainStyledAttributes(Context.java:744)
at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:842)
at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:809)
at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:633)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:259)
at com.hectorwithc.training_handbook.MainActivity.<init>(MainActivity.kt:48)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:45)
at android.app.Instrumentation.newActivity(Instrumentation.java:1253)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3353)
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)
W/System: A resource failed to call close.
MainActivity.kt
package com.hectorwithc.training_handbook
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import android.widget.Toast
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nextButton = findViewById<Button>(R.id.nextButtonId)
val backButton = findViewById<Button>(R.id.startButtonId)
var currentStep = 0
var maxStep = 6
var minStep = 0
nextButton.setOnClickListener {
if (currentStep === 0) {
currentStep = 1
step1()
} else if (currentStep === 1) {
currentStep = 1
step2()
} else if (currentStep === 2) {
currentStep = 1
step3()
} else if (currentStep === 3) {
currentStep = 1
step4()
} else if (currentStep === 4) {
currentStep = 1
step5()
} else if (currentStep === 5) {
currentStep = 1
step6()
}
}
backButton.setOnClickListener {
}
}
val textInstruction = findViewById<TextView>(R.id.textInstruction1)
val textMEquipment = findViewById<TextView>(R.id.textMEquipment)
val textMNumber = findViewById<TextView>(R.id.textMNumber)
val textMName = findViewById<TextView>(R.id.textMName)
fun step1() {
textInstruction.text = "Step 1"
}
fun step2() {
textInstruction.text = "Step 2"
}
fun step3() {
textInstruction.text = "Step 3"
}
fun step4() {
textInstruction.text = "Step 4"
}
fun step5() {
textInstruction.text = "Step 5"
}
fun step6() {
textInstruction.text = "Step 6"
}
}
activity_main.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"
tools:context=".MainActivity">
<Button
android:id="@ id/nextButtonId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginEnd="96dp"
android:layout_marginBottom="24dp"
android:text="Next"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textMEquipment"
app:layout_constraintVertical_bias="0.806" />
<Button
android:id="@ id/startButtonId"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginEnd="96dp"
android:layout_marginBottom="24dp"
android:text="Back"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textMEquipment"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="@ id/textView"
android:layout_width="378dp"
android:layout_height="38dp"
android:layout_marginTop="14dp"
android:background="#1D88F4"
android:text="Training Handbook"
android:textAlignment="center"
android:textColor="#FFFFFF"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@ id/textMEquipment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M Equipment: "
android:textColor="#85000000"
android:textSize="28sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView" />
<TextView
android:id="@ id/textMName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M Name: "
android:textColor="#85000000"
android:textSize="28sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textMNumber" />
<TextView
android:id="@ id/textMNumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="M Number: "
android:textColor="#85000000"
android:textSize="28sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textMEquipment" />
<TextView
android:id="@ id/textInstruction1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="None"
android:textColor="#E4000000"
android:textSize="48sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@ id/textMEquipment"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@ id/textView" />
</androidx.constraintlayout.widget.ConstraintLayout>
CodePudding user response:
You have added initialization outside of oncreate scope. Try this
import android.os.Bundle
import android.widget.Button
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
class MainActivity : AppCompatActivity() {
lateinit var textInstruction: TextView //Add Here
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val nextButton = findViewById<Button>(R.id.nextButtonId)
val backButton = findViewById<Button>(R.id.startButtonId)
textInstruction = findViewById(R.id.textInstruction1) //Remove val from here
val textMEquipment = findViewById<TextView>(R.id.textMEquipment)
val textMNumber = findViewById<TextView>(R.id.textMNumber)
val textMName = findViewById<TextView>(R.id.textMName)
var currentStep = 0
var maxStep = 6
var minStep = 0
nextButton.setOnClickListener {
if (currentStep === 0) {
currentStep = 1
step1()
} else if (currentStep === 1) {
currentStep = 1
step2()
} else if (currentStep === 2) {
currentStep = 1
step3()
} else if (currentStep === 3) {
currentStep = 1
step4()
} else if (currentStep === 4) {
currentStep = 1
step5()
} else if (currentStep === 5) {
currentStep = 1
step6()
}
}
backButton.setOnClickListener {
}
}
fun step1() {
textInstruction.text = "Step 1"
}
fun step2() {
textInstruction.text = "Step 2"
}
fun step3() {
textInstruction.text = "Step 3"
}
fun step4() {
textInstruction.text = "Step 4"
}
fun step5() {
textInstruction.text = "Step 5"
}
fun step6() {
textInstruction.text = "Step 6"
}
}
CodePudding user response:
Sorry to everyone but I managed to fix it, it turns out I had not imported the right thing at the top.