Home > database >  Android ViewModel class can't be initialized
Android ViewModel class can't be initialized

Time:10-24

This is an InstantiationException for a ViewModel class defined in the MainActivity. This is an app for counting timer which uses LiveData with ViewModel to display timer on the screen.

This is the error message.

Caused by: java.lang.InstantiationException: java.lang.Class<ca.sheridancollege.fangyux.livedata_counterappausing.CounterViewModel> cannot be instantiated
        at java.lang.Class.newInstance(Native Method)
        at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.kt:202)
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:322) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:304) 
        at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.kt:278) 
        at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.kt:128) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:187) 
        at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.kt:153) 
        at ca.sheridancollege.fangyux.livedata_counterappausing.MainActivity.onCreate(MainActivity.kt:15) 
        at android.app.Activity.performCreate(Activity.java:7994) 
        at android.app.Activity.performCreate(Activity.java:7978) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422) 
        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) 

This is my MainActivity

package ca.sheridancollege.fangyux.livedata_counterappausing

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import kotlinx.android.synthetic.main.activity_main.*

class MainActivity : AppCompatActivity() {

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

        val counterModel=ViewModelProvider(this).get(CounterViewModel::class.java)

        counterModel.startTimer()

        counterModel.getTimeLeft().observe(this, Observer {
            txt_counter.text = it.toString()
        })
    }
}

And this is my ViewModel class

package ca.sheridancollege.fangyux.livedata_counterappausing

import android.os.CountDownTimer
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

abstract class CounterViewModel : ViewModel() {

    lateinit var timer:CountDownTimer
    private val timeLeft=MutableLiveData<Int>()

    fun startTimer(){
        timer=object:CountDownTimer(10000,1000){
            override fun onTick(p0: Long) {
                timeLeft.value=(p0/1000).toInt()
            }

            override fun onFinish() {

            }
        }.start()
    }

    fun getTimeLeft():LiveData<Int>{
        return timeLeft
    }
}

Can anyone please help me with it? Can anyone please help me with it? Can anyone please help me with it? Can anyone please help me with it? Can anyone please help me with it? Can anyone please help me with it?

CodePudding user response:

Your class is declared as

abstract class CounterViewModel : ViewModel()

You cannot instantiate an abstract class, so remove the abstract keyword if you want to instantiate it.

  • Related