M
M
Maxim Siomin2021-04-26 20:37:20
Android
Maxim Siomin, 2021-04-26 20:37:20

Error while trying to create object: Runnable?

androidviewmodel
class AppViewModel (application: Application) : AndroidViewModel(application) {
    var currentTemperature : MutableLiveData<Int> = MutableLiveData()
    lateinit var temperatureHandler: Handler
    private val applicationContext: Context by lazy { getApplication() }

    private val updateTemperature = object : Runnable {
        override fun run() {
            while (true) {
                temperatureHandler.postDelayed(this, 1000)
                currentTemperature.value = getBatteryTemperature(applicationContext)
            }
        }
    }

    init {
        updateTemperature.run()
    }
}

Mistake
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.maximsiomin.batterytemperature/com.maximsiomin.batterytemperature.ui.MainActivity}: java.lang.RuntimeException: Cannot create an instance of class com.maximsiomin.batterytemperature.viewmodel.AppViewModel
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
        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:2016)
        at android.os.Handler.dispatchMessage(Handler.java:107)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
     Caused by: java.lang.RuntimeException: Cannot create an instance of class com.maximsiomin.batterytemperature.viewmodel.AppViewModel
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:238)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130)
        at com.maximsiomin.batterytemperature.ui.MainActivity$temperature$2.invoke(MainActivity.kt:12)
        at com.maximsiomin.batterytemperature.ui.MainActivity$temperature$2.invoke(MainActivity.kt:11)
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74)
        at com.maximsiomin.batterytemperature.ui.MainActivity.getTemperature(Unknown Source:2)
        at com.maximsiomin.batterytemperature.ui.MainActivity.onCreate(MainActivity.kt:20)
        at android.app.Activity.performCreate(Activity.java:7802)
        at android.app.Activity.performCreate(Activity.java:7791)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance0(Native Method)
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230)
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
        at com.maximsiomin.batterytemperature.ui.MainActivity$temperature$2.invoke(MainActivity.kt:12) 
        at com.maximsiomin.batterytemperature.ui.MainActivity$temperature$2.invoke(MainActivity.kt:11) 
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) 
        at com.maximsiomin.batterytemperature.ui.MainActivity.getTemperature(Unknown Source:2) 
        at com.maximsiomin.batterytemperature.ui.MainActivity.onCreate(MainActivity.kt:20) 
        at android.app.Activity.performCreate(Activity.java:7802) 
        at android.app.Activity.performCreate(Activity.java:7791) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
     Caused by: kotlin.UninitializedPropertyAccessException: lateinit property temperatureHandler has not been initialized
        at com.maximsiomin.batterytemperature.viewmodel.AppViewModel.getTemperatureHandler(AppViewModel.kt:12)
        at com.maximsiomin.batterytemperature.viewmodel.AppViewModel$updateTemperature$1.run(AppViewModel.kt:18)
        at com.maximsiomin.batterytemperature.viewmodel.AppViewModel.<init>(AppViewModel.kt:25)
        at java.lang.reflect.Constructor.newInstance0(Native Method) 
        at java.lang.reflect.Constructor.newInstance(Constructor.java:343) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:230) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:164) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:130) 
        at com.maximsiomin.batterytemperature.ui.MainActivity$temperature$2.invoke(MainActivity.kt:12) 
        at com.maximsiomin.batterytemperature.ui.MainActivity$temperature$2.invoke(MainActivity.kt:11) 
        at kotlin.SynchronizedLazyImpl.getValue(LazyJVM.kt:74) 
        at com.maximsiomin.batterytemperature.ui.MainActivity.getTemperature(Unknown Source:2) 
        at com.maximsiomin.batterytemperature.ui.MainActivity.onCreate(MainActivity.kt:20) 
        at android.app.Activity.performCreate(Activity.java:7802) 
        at android.app.Activity.performCreate(Activity.java:7791) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
        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:2016) 
        at android.os.Handler.dispatchMessage(Handler.java:107) 
        at android.os.Looper.loop(Looper.java:214) 
        at android.app.ActivityThread.main(ActivityThread.java:7356) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 

If commented out
init {
        //updateTemperature.run()
 }

That will work. That is, when you try to call the runnable object, an error occurs. On stackoverflow I didn’t find such a topic at all, how to fix it
Here is the MainActivity code just in case
class MainActivity : AppCompatActivity() {
    private val temperature by lazy { ViewModelProviders.of(this).get(AppViewModel::class.java)}

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

        val temperatureTextView: TextView = findViewById(R.id.temperature_number)
        val temperatureObserver = Observer<Int> { currentTemperature -> temperatureTextView.text = currentTemperature.toString() }
        temperature.currentTemperature.observe(this, temperatureObserver)
    }
}


What can be done?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2021-04-26
@MaxSiominDev

No, not true, an error occurred while creating the viewmodel because UninitializedPropertyAccessException: lateinit property temperatureHandler has not been initialized.
You promised (lateinit) the compiler that you would initialize the temperatureHandler later, and you didn't. When accessing this property, runtime punished you with a fall. Initialize it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question