2023-10-24 06:21:59.622 13402-13402 AndroidRuntime com.example.labtest1 D Shutting down…
- User
2023-10-24 06:21:59.622 13402-13402 AndroidRuntime com.example.lab_test_1 D Shutting down VM 2023-10-24 06:21:59.625 13402-13402 AndroidRuntime com.example.lab_test_1 E FATAL EXCEPTION: main Process: com.example.lab_test_1, PID: 13402 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lab_test_1/com.example.lab_test_1.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:3689) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) at android.app.ActivityThreadMethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) 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:206) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174) at android.content.Context.obtainStyledAttributes(Context.java:999) at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:922) at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:889) at androidx.appcompat.app.AppCompatDelegateImpl.findViewById(AppCompatDelegateImpl.java:691) at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:264) at com.example.lab_test_1.MainActivity.(MainActivity.kt:17) 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:1378) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3676) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3922) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:139) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:96) at android.app.ActivityThreadMethodAndArgsCaller.run(RuntimeInit.java:552) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:971) ---------------------------- PROCESS ENDED (13402) for package com.example.lab_test_1 ----------------------------
- Assistant
The log you provided indicates that there was a fatal exception in your application. The error occurred during the creation of an activity (MainActivity). Here is a breakdown of the error message:
Shutting down VM: This is just a log message indicating that the virtual machine is being shut down.FATAL EXCEPTION: main: This is an indication that a fatal exception occurred in the main thread of your application.Process: com.example.lab_test_1, PID: 13402: This shows the process ID (PID) of your application.java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.lab_test_1/com.example.lab_test_1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference: This is the main error message. It suggests that there was a NullPointerException thrown while trying to instantiate the MainActivity activity. Specifically, it seems there was an issue accessing the application context.
- User
- Assistant
The issue in your code is that you are trying to find views (TextViews) using
findViewByIdoutside of any method or lifecycle callback. You should only access views after callingsetContentViewto ensure that the views have been inflated and are available.To fix the issue, move the following lines inside the
onCreatemethod, aftersetContentView(R.layout.activity_main):val result0TextView = findViewById<TextView>(R.id.result0) val result1TextView = findViewById<TextView>(R.id.result1) val result2TextView = findViewById<TextView>(R.id.result2)So, your updated code should look like this:
val argAx: EditText? = null val argAy: EditText? = null val argAz: EditText? = null var count0 = 0.0 var count1 = 0.0 var count2 = 0.0 override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val result0TextView = findViewById<TextView>(R.id.result0) val result1TextView = findViewById<TextView>(R.id.result1) val result2TextView = findViewById<TextView>(R.id.result2) argAx = findViewById(R.id.argAx); argAy = findViewById(R.id.argAy); argAz = findViewById(R.id.argAz); val argAxEditText = findViewById<EditText>(R.id.argAx) val argAyEditText = findViewById<EditText>(R.id.argAy) val argAzEditText = findViewById<EditText>(R.id.argAz) // Rest of your code... }