102

I am following this documentation to learn about LiveData and ViewModel. In the doc, the ViewModel class has constructor as such,

public class UserModel extends ViewModel {
  private MutableLiveData<User> user;

  @Inject UserModel(MutableLiveData<User> user) {
    this.user = user;
  }

  public void init() {
    if (this.user != null) {
      return;
    }
    this.user = new MutableLiveData<>();
  }

  public MutableLiveData<User> getUser() {
    return user;
  }
}

However, when I run the code, I get exception:

final UserViewModelviewModel = ViewModelProviders.of(this).get(UserViewModel.class);

Caused by: java.lang.RuntimeException: Cannot create an instance of class UserViewModel Caused by: java.lang.InstantiationException: java.lang.Class has no zero argument constructor

PatrickNLT
  • 4,075
  • 1
  • 25
  • 32
Prabin Timsina
  • 2,131
  • 3
  • 16
  • 27

24 Answers24

94

In my case as I'm using HILT, it was lacking one annotation above the Fragment that has a ViewModel: @AndroidEntryPoint

@AndroidEntryPoint
class BestFragment : Fragment() { 
....

Of course in your ViewModel class you also need to Annotate with what HILT needs: @ViewModelInject

class BestFragmentViewModel @ViewModelInject constructor(var userManager: UserManager) : ViewModel() {
....
}
Dimitri Leite
  • 1,791
  • 13
  • 16
68

While initializing subclasses of ViewModel using ViewModelProviders, by default it expects your UserModel class to have a zero argument constructor. In your case your constructor has the argument MutableLiveData<User> user.

One way to fix this is to have a default no arg constructor for your UserModel.

Otherwise, if you want to have a non-zero argument constructor for your ViewModel class, you may have to create a custom ViewModelFactory class to initialise your ViewModel instance, which implements the ViewModelProvider.Factory interface.

I have not tried this yet, but here's a link to an excellent Google sample for this: github.com/googlesamples/android-architecture-components. Specifically, check out this class GithubViewModelFactory.java for Java code and this class GithubViewModelFactory.kt for the corresponding Kotlin code.

medavox
  • 175
  • 2
  • 11
Shahbaz Ahmed
  • 1,190
  • 10
  • 13
  • 1
    Discussion around this: https://www.reddit.com/r/androiddev/comments/6bw1jj/architecture_components_introduction_google_io_17/ – DarkLeafyGreen May 27 '17 at 08:36
  • @LostinOWL I am getting the same error, I have checked DaggerAppComponent class and it's generating all the dependency graph properly. – Shashank Srivastava Apr 02 '18 at 22:18
  • Here is the Google sample before they changed it to kotlin: https://github.com/googlesamples/android-architecture-components/blob/b1a194c1ae267258cd002e2e1c102df7180be473/GithubBrowserSample/app/src/main/java/com/android/example/github/viewmodel/GithubViewModelFactory.java – hexicle Apr 27 '18 at 21:40
  • @ShashankSrivastava Please help: https://stackoverflow.com/questions/66396049/how-can-i-tell-dagger-hilt-that-use-my-own-viewmodelfactory-instead-of-default –  Feb 27 '21 at 06:01
  • 1
    If you are using dagger hilt and version 2.31 or higher then don't use "ViewModelInject" in view model class. Please follow below instruction. Add @HiltViewModel on top of class Use Inject intead of ViewModelInject – Samset Jul 26 '21 at 07:20
37

ViewModelFactory that will provide us a right ViewModel from ViewModelModule

public class ViewModelFactory implements ViewModelProvider.Factory {
    private final Map<Class<? extends ViewModel>, Provider<ViewModel>> viewModels;

    @Inject
    public ViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> viewModels) {
        this.viewModels = viewModels;
    }

    @Override
    public <T extends ViewModel> T create(Class<T> modelClass) {
        Provider<ViewModel> viewModelProvider = viewModels.get(modelClass);

        if (viewModelProvider == null) {
            throw new IllegalArgumentException("model class " + modelClass + " not found");
        }

        return (T) viewModelProvider.get();
    }
}

ViewModelModule is responsible for binding all over ViewModel classes into
Map<Class<? extends ViewModel>, Provider<ViewModel>> viewModels

@Module
public abstract class ViewModelModule {

    @Binds
    abstract ViewModelProvider.Factory bindViewModelFactory(ViewModelFactory viewModelFactory); 
    //You are able to declare ViewModelProvider.Factory dependency in another module. For example in ApplicationModule.

    @Binds
    @IntoMap
    @ViewModelKey(UserViewModel.class)
    abstract ViewModel userViewModel(UserViewModel userViewModel);
    
    //Others ViewModels
}

ViewModelKey is an annotation for using as a key in the Map and looks like

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@MapKey
@interface ViewModelKey {
    Class<? extends ViewModel> value();
}

Now you are able to create ViewModel and satisfy all necessary dependencies from the graph

public class UserViewModel extends ViewModel {
    private UserFacade userFacade;

    @Inject
    public UserViewModel(UserFacade userFacade) { // UserFacade should be defined in one of dagger modules
        this.userFacade = userFacade;
    }
} 

Instantiating ViewModel

public class MainActivity extends AppCompatActivity {

    @Inject
    ViewModelFactory viewModelFactory;
    UserViewModel userViewModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ((App) getApplication()).getAppComponent().inject(this);

        userViewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel.class);

    }
}

And do not forger to add ViewModelModule into modules list

@Singleton
@Component(modules = {ApplicationModule.class, ViewModelModule.class})
public interface ApplicationComponent {
    //
}
lincollincol
  • 762
  • 2
  • 12
  • 23
yoAlex5
  • 29,217
  • 8
  • 193
  • 205
  • I am getting an error: `[dagger.android.AndroidInjector.inject(T)] java.util.Map,javax.inject.Provider> cannot be provided without an @Provides-annotated method.` – Levon Petrosyan Jul 06 '18 at 13:22
  • It is hard to say what the problem is without seeing the whole project, I can assume that 1. child of ViewModel was not declared in `ViewModelModule`. 2. `ViewModelModule ` was not added to `Component` – yoAlex5 Jul 06 '18 at 14:25
  • @LevonPetrosyan i got same issues, create constructor with no argument and @ Inject annotation – silentsudo Jul 17 '18 at 13:28
  • @silentsudo my issue was that I was Inject -ing viewModel in Fragment,but thx anyway :)) – Levon Petrosyan Jul 17 '18 at 13:30
  • 1
    I think this is the best explanation of the above implementation: https://medium.com/@marco_cattaneo/android-viewmodel-and-factoryprovider-good-way-to-manage-it-with-dagger-2-d9e20a07084c – Maciej Beimcik Sep 11 '18 at 19:57
  • @yoAlex5 Thanks, bud! Saved my day. – code Apr 13 '19 at 17:31
  • can you provide the same for Kotlin – Venkatesh Kashyap Dec 11 '19 at 07:59
  • In newer android versions this is deprecated: `((App) getApplication()).getAppComponent().inject(this);` `userViewModel = ViewModelProviders.of(this, viewModelFactory).get(UserViewModel.class);` This worked for me `userViewModel = new ViewModelProvider(this, viewModelFactory).get(UserViewModel.class);` – Johana Lopez 1327 Jun 19 '20 at 10:10
32

For Hilt:

Simple add @AndroidEntryPoint for main acticity and fragments, and @HiltViewModel for viewModels

Example after:

@HiltViewModel
class SplashViewModel @Inject constructor(

@AndroidEntryPoint
class SplashFragment : Fragment() {
    private lateinit var b: SplashFragmentBinding
    private val vm: SplashViewModel by viewModels()

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
Fortran
  • 2,218
  • 2
  • 27
  • 33
11

I had some issues with @ViewModelInject since it has been deprecated using HILT. To solve the problem change this code:

class MainViewModel @ViewModelInject constructor(
    val mainRepository: MainRepository
): ViewModel()

with:

@HiltViewModel
class MainViewModel @Inject constructor(
    val mainRepository: MainRepository
): ViewModel()

Of course, remember to add the @AndroidEntryPoint annotation above your fragment or activity (wherever you are instantiating your ViewModel) like this:

@AndroidEntryPoint
class UsageFragment : Fragment(R.layout.fragment_usage) {
   .
   .
   .
}

Ultimate tip:

You can immediately see if HILT is working looking if there are the icons on the left in your ViewModel.

Here it does not work:

enter image description here

Here it does work:

enter image description here

If you don't see them after updating the code click on Build -> Rebuild Project

Mattia Ferigutti
  • 2,608
  • 1
  • 18
  • 22
  • 1
    After an hour of trying different alternatives, only this worked fine for me. Thank you very much. – Ivette May 11 '21 at 11:30
  • 1
    This worked for me, But Also I would like to mention maybe this could be in new version of HILT library i.e "hilt_version = '2.35'" In the earlier version i.e "hilt_version = '2.28.3-alpha'" I guess this would not be necessary – Snehal Gongle May 28 '21 at 05:35
7

Early in 2020, Google have deprecated the ViewModelProviders class, in version 2.2.0 of the androidx lifecycle library.

It's no longer necessary to use ViewModelProviders to create an instance of a ViewModel, you can pass your Fragment or Activity instance to the ViewModelProvider constructor instead.

If you use the code like:

val viewModel = ViewModelProviders.of(this).get(CalculatorViewModel::class.java)

you'll get a warning that ViewModelProviders has been deprecated.

You can instead do:

val viewModel = ViewModelProvider(this).get(CalculatorViewModel::class.java)

Or alternatively, to use a delegate, make the following changes.

  1. In the build.gradle (Module: app) file, use version 2.2.0 of the lifecycle components: implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'

    Also add implementation "androidx.activity:activity-ktx:1.1.0"

    If you want to use the ViewModel from a Fragment instead, use

    implementation "androidx.fragment:fragment-ktx:1.2.2"

    fragment-ktx automatically includes activity-ktx, so you don't need to specify both in the dependencies.

  2. You need to specify Java 8 in the android section :

android {
  compileSdkVersion 28
  defaultConfig {
    applicationId "com.kgandroid.calculator"
    minSdkVersion 17
    targetSdkVersion 28
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
  }
  
  buildTypes {
    release {
      minifyEnabled false
      proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
  }
         
  kotlinOptions {
    jvmTarget = "1.8"
  }
}
  1. In your Fragment or Activity, change the import to:

    import androidx.activity.viewModels

  2. The code to create a ViewModel then becomes:

    val viewModel: CalculatorViewModel by viewModels()

    instead of

    val viewModel = ViewModelProviders.of(this).get(CalculatorViewModel::class.java)

    Use the viewModel object as :

    val viewModel: CalculatorViewModel by viewModels()

    viewModel.newNumber.observe(this, Observer { stringResult -> newNumber.setText(stringResult) })

where newNumer is a LiveData object

In a Fragment that you want to share the Activity's ViewModel, you'd use

`val viewModel: CalculatorViewModel by activityViewModels()`

**That's the equivalent of passing the Activity instance in the (deprecated) 
ViewModelProviders.of() function.**
medavox
  • 175
  • 2
  • 11
kgandroid
  • 5,507
  • 5
  • 39
  • 69
  • I am getting a compiler error while creating an instance of viewmodel at this line val viewModel: CalculatorViewModel by viewModels() I want the CalculatorViewModel to be AndroidViewModel because i want context – yeshu Apr 26 '20 at 13:38
  • Even if you extend AndroidViewModel, this will work.What is the exact error are you getting? – kgandroid Apr 26 '20 at 15:56
  • Classifier 'CalculatorViewModel' doesn't have companion object and, this must be initialized here. – yeshu May 02 '20 at 14:50
  • I haven't encountered any such problem. Post a question and give the link here. I will have a look at that. – kgandroid May 03 '20 at 08:28
5

2020-07-29 10:13:25

For lifecycle_version = '2.2.0' ViewProviders.of API is deprecated . It`s my situation :

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

    private var repository: UserRepository

    val allUsers: LiveData<List<User>>
......


error:
val userViewModel = ViewModelProvider(this).get(MainActivityViewModel::class.java)

success:
val factory = ViewModelProvider.AndroidViewModelFactory.getInstance(application)
userViewModel = ViewModelProvider(this,factory).get(MainActivityViewModel::class.java)

Pass application by api ViewModelProvider.AndroidViewModelFactory.getInstance


javakam
  • 127
  • 1
  • 5
5

I had the same error. I'm using Hilt, and in my case it was a missing second hilt compiler dependency

now i have both:

kapt com.google.dagger:hilt-android-compiler:#version

and

kapt androidx.hilt:hilt-compiler:#version

in my app level build.gradle file and it works.

com.google.dagger:hilt-android-compiler is needed when your using the Hilt Android Gradle plugin (see docs) and androidx.hilt:hilt-compiler:#version is apparently needed when you want Hilt and Jetpack integration, like injecting Android Jetpack ViewModel (see docs)

nir
  • 302
  • 3
  • 12
4

if you're using hilt, you probably might forgot to annotate your activity or fragment with @AndroidEntryPoint

Sirisha
  • 423
  • 4
  • 12
3

The most common reason for this failure is Missing @AndroidEntryPoint at the start of your Fragment/Activity as shown below:

@AndroidEntryPoint
class MyFragment : Fragment {
val viewModel by viewModels<MyViewModel>()
}

Similarly, you ViewModel should be annotated by HiltViewModel as shown following:

@HiltViewModel
class MyViewModel@Inject constructor(
private val var1: Type1
) : ViewModel()
Tarun Deep Attri
  • 8,174
  • 8
  • 41
  • 56
3

If you are using navigation-compose and calling your screen inside the NavHost block, the hilt can't inject the view model. For this, you can use this way;

    NavHost(navHostController, startDestination = "HomeScreen") {
    composable("HomeScreen") {
        HomeScreen(homeScreenViewModel = hiltViewModel())
    }
}

Don't forget to add this dependency for hiltViewModel() -> implementation("androidx.hilt:hilt-navigation-compose:1.0.0-alpha02")

Cafer Mert Ceyhan
  • 1,640
  • 1
  • 13
  • 17
2

For everyone who has this problem, I encountered it this way:
1- In Your ViewModel, don't create a constructor, just create a function that takes a Context and your other parameters

public class UserModel extends ViewModel {
  private Context context; 
  private ..........;

  public void init(Context context, ......) {
    this.context = context;
    this..... = ....;
  }


  // Your stuff
}

2- In your activity:


UserViewModel viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
viewModel.init(this, .....);

// You can use viewModel as you want

3- In your fragment

UserViewModel viewModel = ViewModelProviders.of(getActivity()).get(UserViewModel.class);
viewModel.init(getContext(), .....);

// You can use viewModel as you want
NOUAILI
  • 21
  • 2
2

For Koin:

I had this issue, turns out I just imported viewModels() from AndroidX instead of viewModel() from Koin

Merthan Erdem
  • 5,598
  • 2
  • 22
  • 29
  • 1
    For whom looking for full qualifier for `viewModel()` (like me), It is `org.koin.androidx.viewmodel.ext.android.viewModel` in Koin 3.1.5. My Android Studio didn't find `viewModel` automatically. I imported it manually. – khcpietro Apr 22 '22 at 09:24
2

In my case, the id 'dagger.hilt.android.plugin' plugin was missing, implemented it and now it is working.

So there are some steps which we have to check

  1. @HiltAndroidApp at the application class, and the application is registered in the Android Manifest

  2. Activity, as well as the Fragment, should be annotated with @AndroidEntryPoint

  3. Check the dependencies and plugins are the right implemented, such as now in my case the current dependencies are:

implementations:

implementation("com.google.dagger:hilt-android:2.44") implementation("androidx.hilt:hilt-navigation-fragment:1.0.0") implementation ("androidx.lifecycle:lifecycle-livedata-ktx:2.5.1") kapt("com.google.dagger:hilt-android-compiler:2.44")

Plugin: id 'dagger.hilt.android.plugin'

classpath: classpath("com.google.dagger:hilt-android-gradle-plugin:$hiltVersion")

1

I wrote a library that should make achieving this more straightforward and way cleaner, no multibindings or factory boilerplate needed, while also giving the ability to further parametrise the ViewModel at runtime: https://github.com/radutopor/ViewModelFactory

@ViewModelFactory
class UserViewModel(@Provided repository: Repository, userId: Int) : ViewModel() {

    val greeting = MutableLiveData<String>()

    init {
        val user = repository.getUser(userId)
        greeting.value = "Hello, $user.name"
    }    
}

In the view:

class UserActivity : AppCompatActivity() {
    @Inject
    lateinit var userViewModelFactory2: UserViewModelFactory2

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_user)
        appComponent.inject(this)

        val userId = intent.getIntExtra("USER_ID", -1)
        val viewModel = ViewModelProviders.of(this, userViewModelFactory2.create(userId))
            .get(UserViewModel::class.java)

        viewModel.greeting.observe(this, Observer { greetingText ->
            greetingTextView.text = greetingText
        })
    }
}
Radu Topor
  • 1,504
  • 1
  • 11
  • 12
1

i had the same issue, fixed it by adding navigation ui library to my project:

implementation 'androidx.navigation:navigation-ui-ktx:2.2.2'
Usama Saeed US
  • 984
  • 11
  • 18
0

Create a constructor with out any arguments i.e.

Default/ No-arg constructor

in the viewmodel class .

In my case, I forgot to generate this constructor and wasted 30 minutes when I'm learning - after that it worked for me.

m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
0

With regards to the accepted answer, if you are using Hilt and you just added your ViewModel, don't forget to rebuild your project. Simply running the project does not create the needed factory classes (which are supposed to be automatically generated), as discovered the hard way.

The classes below did not exist before the rebuild:

enter image description here

Aloha
  • 864
  • 18
  • 40
0

If you are using dagger hilt and version 2.31 or higher then don't use "ViewModelInject" in view model class. Dagger is providing new way to use viewmodel so please follow below instruction.

1: Add @HiltViewModel on top of class 2: Use Inject intead of ViewModelInject

@HiltViewModel
class AuthViewModel @Inject constructor( 
private val authRepository: AuthRepository,
    ...
) : ViewModel() 
{...}
Samset
  • 109
  • 7
0

Android Developer documentation has examples for how to create a custom ViewModel factory: https://developer.android.com/topic/libraries/architecture/viewmodel#viewmodel-with-dependencies

Basically if you are using Kotlin you define the ViewModel factory inside of a companion object - here they are passing in a repository and saved state to the constructor of the ViewModel but you can pass in whatever dependencies your ViewModel needs:

class MyViewModel(
    private val myRepository: MyRepository,
    private val savedStateHandle: SavedStateHandle
) : ViewModel() {

    // ViewModel logic
    // ...

    // Define ViewModel factory in a companion object
    companion object {

        val Factory: ViewModelProvider.Factory = object : ViewModelProvider.Factory {
            @Suppress("UNCHECKED_CAST")
            override fun <T : ViewModel> create(
                modelClass: Class<T>,
                extras: CreationExtras
            ): T {
                // Get the Application object from extras
                val application = checkNotNull(extras[APPLICATION_KEY])
                // Create a SavedStateHandle for this ViewModel from extras
                val savedStateHandle = extras.createSavedStateHandle()

                return MyViewModel(
                    (application as MyApplication).myRepository,
                    savedStateHandle
                ) as T
            }
        }
    }
}

Then you use your factory to get the ViewModel from your Activity:

class MyActivity : AppCompatActivity() {

    private val viewModel: MyViewModel by viewModels { MyViewModel.Factory }

    // Rest of Activity code
}
Matt
  • 688
  • 5
  • 12
0

In my case, I was using Koin and getting this error. I was creating my viewModel isntance like this:

 val viewModel = ViewModelProvider(requireActivity())[HomeViewModel::class.java] 

All you need to do is initialize your viewmodel like this:

import org.koin.android.ext.android.get

val viewModel = get<HomeViewModel>()
Yunis Rasulzade
  • 325
  • 3
  • 12
-1

Just Extend "AndroidViewModel" instead of "ViewModel"

Djoxy
  • 54
  • 4
-2

The problem can be resolved by extending UserModel from AndroidViewModel which is application context aware ViewModel and requires Application parameter-only constructor. (documentation)

Ex- (in kotlin)

class MyVm(application: Application) : AndroidViewModel(application)

This works for version 2.0.0-alpha1.

rushi
  • 142
  • 1
  • 12
  • 2
    The AndroidViewModel accepts only 1 argument, which is Application. The only way around either versions of ViewModel is to create a Factory. – zuko Jul 24 '18 at 23:11
-3

If you have parameter in constructor then :

DAGGER 2 public constructor for @inject dependency

@Inject
public UserViewModel(UserFacade userFacade)
{ 
    this.userFacade = userFacade;
}

Otherwise dagger 2 will send you error "can not instantiate viewmodel object"

  • Why do people write comments just to comment? He has @Inject, but his problem is already noted above, and it is not what you wrote about. – Slobodan Antonijević Feb 26 '19 at 11:55
  • It'll be good to write with an example, above answers were not clear much to me, so i wrote one that i could understands easily and other persons as well. – Shahid Ahmad Feb 27 '19 at 10:14