0

After dragged the SeekBar to the design I want to use it in the

MainActivity.kt:

    package com.example.flashlight
    
    import android.content.Context
    import android.hardware.camera2.CameraAccessException
    import android.hardware.camera2.CameraManager
    import android.os.Bundle
    import android.os.Handler
    import android.os.Looper
    import android.view.View
    import android.widget.SeekBar
    import androidx.appcompat.app.AppCompatActivity
    
    class MainActivity : AppCompatActivity() {
        var flashLightStatus: Boolean = false
        var counter = 0;
        val seek = findViewById<SeekBar>(R.id.seekBar)

Application can't pass the line:

    val seek = findViewById<SeekBar>(R.id.seekBar)

It looks like it's trying to running the app but then quit.

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:id="@+id/seekBar"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
        <SeekBar
            android:id="@+id/seekBar"
            android:layout_width="377dp"
            android:layout_height="18dp"
            android:layout_marginStart="21dp"
            android:layout_marginTop="240dp"
            android:layout_marginEnd="13dp"
            android:visibility="visible"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:visibility="visible"
            tools:ignore="DuplicateIds" />
    
    </androidx.constraintlayout.widget.ConstraintLayout>

In the design:

design

Logger show this error:

FATAL EXCEPTION: main Process: com.example.flashlight, PID: 3778 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.flashlight/com.example.flashlight.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:3910) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4197) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) 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:2434) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8611) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133) 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:190) at android.view.ContextThemeWrapper.getTheme(ContextThemeWrapper.java:174) at android.content.Context.obtainStyledAttributes(Context.java:809) 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.flashlight.MainActivity.(MainActivity.kt:16) 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:1273) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3897) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4197)  at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)  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:2434)  at android.os.Handler.dispatchMessage(Handler.java:106)  at android.os.Looper.loopOnce(Looper.java:226)  at android.os.Looper.loop(Looper.java:313)  at android.app.ActivityThread.main(ActivityThread.java:8611)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:563)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1133) 

Halil Ozel
  • 2,482
  • 3
  • 17
  • 32
Daniel Lip
  • 3,867
  • 7
  • 58
  • 120
  • You need to inflate the layout and set the content view before calling `findViewById`. In order to do this you can define `seek` as `lateinit var`. – dev.bmax May 22 '23 at 10:50
  • @dev.bmax I did at the top: lateinit var seekB: SeekBar then inside the onCreate after the setContentView this: seekB = findViewById(R.id.seekBar) but still the app crash can't start. – Daniel Lip May 22 '23 at 11:00
  • 1
    Looks like you have 2 views with the same ID. Try to delete the first line `android:id="@+id/seekBar"` in `activity_main.xml` (the one that belongs to `ConstraintLayout`). The `DuplicateIds` warning was helpful. It's not recommended to ignore it. – dev.bmax May 22 '23 at 13:53
  • @dev.bmax I removed it and for testing removed the seekbar at all from the activity_main.xml and still getting the fatal error. Process: com.example.flashlight, PID: 24955 java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.flashlight/com.example.flashlight.MainActivity}: java.lang.InstantiationException: java.lang.Class cannot be instantiated – Daniel Lip May 22 '23 at 18:24
  • @dev.bmax could be beacuse there is nothing about the flashlight in the activity_main.xml ? in this link is the full code of MainActivity.kt I didn't have this fatal error/s before only after started messing with the seekbar but even when removing all the seekbar parts the fatal error exist. https://pastebin.com/ksahuMCJ – Daniel Lip May 22 '23 at 18:26
  • `MainActivity` can't be instantiated because you define it as an abstract class. You should remove the keyword `abstract` from the declaration. – dev.bmax May 23 '23 at 07:23

0 Answers0