Menu
Who Do Is
  • Home
  • What
  • How
  • Is
  • Can
  • Are
  • Does
  • Do
  • Why
  • Who
  • Where
  • Which
  • Which
  • Should
  • Will
  • When
  • What’s
  • Did
Who Do Is

[ANSWERED] android – ViewModel has no zero argument constructor

Posted on November 14, 2022

Solution 1 :

Try replacing:

sharedTaskViewModel = ViewModelProvider(this).get(TaskViewModel::class.java)

with

sharedTaskViewModel = ViewModelProvider.AndroidViewModelFactory(application)
    .create(TaskViewModel::class.java)

Problem :

I’m trying to make a simple ToDoList application using the recyclerview and the room library. I’m following the android developers codelab on using room and mvvm architecture and I seem to have hit a stump. I have set up every layer of the application but I’m getting an error when trying to instantiate the ViewModel using the ViewModelProvider. Here is the code from my ViewModel class.

class TaskViewModel(application: Application) : AndroidViewModel(application) {

private val repository : TaskRepository

val allTasks : LiveData<List<Task>>



init{
    val tasksDao = TaskRoomDatabase.getDatabase(application)!!.taskDao()
    repository = TaskRepository(tasksDao)
    allTasks = repository.allTasks

}

fun saveTask(task : Task) = viewModelScope.launch(Dispatchers.IO){
    repository.saveTask(task)
}

}

And here is the code from my MainFragment from which I’m trying to initialize the ViewModel:

class MainFragment() : Fragment() {

private lateinit var recyclerView : RecyclerView
private lateinit var sharedTaskViewModel : TaskViewModel
private lateinit var fab : FloatingActionButton
var tasks = emptyList<Task>()
private lateinit var adapter : MainRecyclerViewAdapter

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_main, container, false)
    fab = view.findViewById(R.id.floatingActionButton)
    //Initialize the recyclerview
    recyclerView = view.findViewById(R.id.main_recyclerview)
    adapter = MainRecyclerViewAdapter()
    recyclerView.adapter = adapter
    recyclerView.layoutManager = LinearLayoutManager(view.context)


    return view
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    sharedTaskViewModel = ViewModelProvider(this).get(TaskViewModel::class.java)


    fab.setOnClickListener {
        fragmentManager!!.beginTransaction().apply {
            replace(R.id.fl_fragment_main, NewTaskFragment())
            commit()
        }
    }

}

}

When I run the code I get this message:

2020-08-09 11:31:00.734 27860-27860/com.robybp.todolist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.robybp.todolist, PID: 27860
java.lang.RuntimeException: Cannot create an instance of class com.robybp.todolist.view_models.TaskViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:187)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at com.robybp.todolist.view.fragments.MainFragment.onViewCreated(MainFragment.kt:45)
 Caused by: java.lang.InstantiationException: java.lang.Class<com.robybp.todolist.view_models.TaskViewModel> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)

Please do forgive me if the question may be “Obslite” but i’m just getting familiar with room and mvvm architecture.
Thank you for understanding.

READ  [ANSWERED] android - What the difference between using Call object and suspend function in the retrofit?
Powered by Inline Related Posts

Comments

Comment posted by stackoverflow.com/a/50487165/9188214

does this answer your question?

Comment posted by stackoverflow.com/questions/63266686/…

hey need help over here –>

Recent Posts

  • How can I play with my cat without toys?
  • What is a bag pipe band called?
  • Are Honda Civics actually fast?
  • Are Yankee candles toxic?
  • How do I pair my Michael Kors smartwatch with my Android?

Recent Comments

No comments to show.

Archives

  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022

Categories

  • ¿Cómo
  • ¿Cuál
  • ¿Cuántas
  • ¿Cuánto
  • ¿Que
  • ¿Quién
  • 90” and 108” so you may have to round up to the nearest size.
  • and delete any Spotify folders from it. Once this is done
  • Android
  • Are
  • At
  • Bei
  • blink
  • C'est
  • Can
  • carbs
  • Comment
  • Did
  • Do
  • Does
  • During
  • For
  • Has
  • How
  • In
  • Is
  • Ist
  • Kann
  • Können
  • nouveau
  • On
  • or 108 inches.2020-08-03
  • Où
  • owning
  • Pourquoi
  • Puis-je
  • Quand
  • Quante
  • Quel
  • Quelle
  • Quelles
  • Quels
  • Qui
  • Should
  • Sind
  • Sollte
  • spiritual
  • tap the downward-facing arrow on the top left. A downward-facing arrow will appear underneath each song in the album; they'll turn green as the download completes.2020-07-28
  • Uncategorized
  • Wann
  • Warum
  • Was
  • Welche
  • Welcher
  • Welches
  • Welke
  • Wer
  • Were
  • What
  • What's
  • When
  • Where
  • Which
  • Who
  • Whose
  • Why
  • Wie
  • Will
  • Wo
  • Woher
  • you will receive two curtains each with the same measurements of width 66"" (168cm) x drop 54""(137cm).
  • you'll see a green downward-facing arrow next to each song.2021-02-26
©2023 Who Do Is | Powered by SuperbThemes & WordPress