14

I am using MVVM, Retrofit, LiveData in my project but I get this error before that I saw these links

Error

java.lang.RuntimeException:       
Unable to start activity ComponentInfo{ir.orangehat.movieinfo/ir.orangehat.movieinfo.application.home.HomeActivity}: java.lang.RuntimeException:

Cannot create an instance of class ir.orangehat.movieinfo.application.home.HomeViewModel
                  

ViewModel

I think the problem is in my constructor

public class HomeViewModel extends AndroidViewModel {

private MovieRepository movieRepository;

public HomeViewModel(@NonNull Application application, Context context, LifecycleOwner lifecycleOwner) {
    super(application);
    movieRepository = new MovieRepository(lifecycleOwner, context);
}

LiveData<List<Movie>> getMovies() {
    return movieRepository.getMovies();
}}

Repository

public class MovieRepository extends BaseRepository {

private LifecycleOwner lifecycleOwner;
private MovieApi movieApi;
private MovieDatabaseHelper movieDatabaseHelper;

public MovieRepository(LifecycleOwner lifecycleOwner, Context context) {
    this.lifecycleOwner = lifecycleOwner;
    movieApi = getRetrofitHelper().getService(MovieApi.class);
    movieDatabaseHelper = new MovieDatabaseHelper(context);
}

public LiveData<List<Movie>> getMovies() {
    LiveData<List<Movie>> moviesLiveData = movieApi.getMovieList();
    moviesLiveData.observe(lifecycleOwner, new Observer<List<Movie>>() {
        @Override
        public void onChanged(@Nullable List<Movie> movieArrayList) {
            movieDatabaseHelper.Save(movieArrayList);
        }
    });

    return movieDatabaseHelper.getAll();
} }

Activity class

public class HomeActivity extends AppCompatActivity {

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

        // the error is here
        HomeViewModel homeViewModel = ViewModelProviders.of(this).get(HomeViewModel.class);

        homeViewModel.getMovies().observe(HomeActivity.this, new Observer<List<Movie>>() {
            @Override
            public void onChanged(@Nullable List<Movie> movieArrayList) {
                String str = null;
                if (movieArrayList != null) {
                    str = Arrays.toString(movieArrayList.toArray());
                }
                Log.e("movies", str);
            }
        });
    }
}

Should I use Dagger in my project and custom factory?

Boken
  • 4,825
  • 10
  • 32
  • 42
Sina Rahimi
  • 323
  • 1
  • 3
  • 13

7 Answers7

19

Quoting the documentation for AndroidViewModel:

Subclasses must have a constructor which accepts Application as the only parameter.

Your constructor does not meet that requirement.

Either:

  • Remove the Context context and LifecycleOwner lifecycleOwner constructor parameters from your HomeViewModel, or

  • Create a ViewModelProvider.Factory that can build your HomeViewModel instances, and use that factory with ViewModelProviders.of()

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I am working on it based on your answer @commonsware – Sina Rahimi Feb 05 '18 at 10:37
  • I have using only one constructor but the issue is coming. when i remove constructor and used viewmodel as base class its working fine. – Jayesh M Nov 27 '19 at 10:08
  • Not working even if the constructor has single "Application application" as parameter.. – Himanshu May 22 '20 at 13:42
  • @Himanshu: Yes, it does. See [this sample Java project](https://gitlab.com/commonsguy/cw-jetpack-java/-/tree/v0.8/DiceLight), as well as [this one](https://gitlab.com/commonsguy/cw-jetpack-java/-/tree/v0.8/SimplePrefs) and [this one](https://gitlab.com/commonsguy/cw-jetpack-java/-/tree/v0.8/Weather). See also the equivalent Kotlin projects [here](https://gitlab.com/commonsguy/cw-jetpack-kotlin/-/tree/v0.8/DiceLight), [here](https://gitlab.com/commonsguy/cw-jetpack-kotlin/-/tree/v0.8/SimplePrefs), and [here](https://gitlab.com/commonsguy/cw-jetpack-kotlin/-/tree/v0.8/Weather). – CommonsWare May 22 '20 at 13:46
  • 1
    @CommonsWare I understand that your answer is correct.. but there are certain other scenarios when this error is thrown even if your ViewModel class has a public constructor with a single Application class parameter. In my case, it was working in one project but not working in another. Looks like there was some dependency issue. Thasnks for your answer though. – Himanshu May 27 '20 at 16:41
6

If you are working with Kotlin and you need to inject a dependency inside your ViewModel constructor to work with like a Repository to get data (the same way we use to do with the Presenter layer) you will need to do the following.

Lets say we have a ViewModel that needs a UseCase/Interactor to be injected in the constructor to get data from.

class MainViewModel(private val itemList:ItemListUseCase):ViewModel() {
...
}

When we try to instantiate this ViewModel in our Activity/Fragment, we tend to do this

Fragment example

   class MainFragment : Fragment() {
    private lateinit var mainViewModel: MainViewModel
    
      override fun onCreate(savedInstanceState: Bundle?) {
            super.onCreate(savedInstanceState)
    
            mainViewModel = requireActivity().run {
                ViewModelProvider(this).get(MainViewModel::class.java)
            }
          ...
        }
}

When we try to run our code, it will crash with a RuntimeException

java.lang.RuntimeException: Cannot create an instance of class com.gaston.example.viewmodel.MainViewModel ... Caused by: java.lang.InstantiationException: java.lang.Class<com.gaston.example.viewmodel.MainViewModel> has no zero argument constructor

This is because we are not injecting the ItemListUseCase when we instantiate our ViewModel

The first thing that comes up is to try to inject it directly from the .get() method of ViewModelProvider

ViewModelProvider(this).get(MainViewModel(ItemListUseCase()))

But if we do this, we will be getting the same error too.

Solution

What we need to understand is that

ViewModelProvider(this).get(MainViewModel::class.java)

ViewModelProvider() is trying to do an instance of the MainViewModel without knowing that it needs a dependency inside its constructor.

To fix this issue, we will need to let ViewModelProvier() know about which dependency we want to inject.

To do this, we need to create a class that will ensure that the instance of that ViewModel needs a dependency to be instantiated.

This class is called a Factory ViewModel class, since it will construct our ViewModel with the dependency it needs to work and then it will let the ViewModelProvier() know which dependency we need to pass to .get(MainViewModel::class.java)

class ViewModelFactory(val requestItemData: ItemListUseCase):ViewModelProvider.Factory {

    override fun <T : ViewModel?> create(modelClass: Class<T>): T {
        return modelClass.getConstructor(ItemListUseCase::class.java).newInstance(requestItemData)
    }
}

Now, we can tell to ViewModelProviers.of() that it needs an instance of ItemListUseCase::class.java to instantiate MainViewModel(ItemListuseCase())

override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        mainViewModel = requireActivity().run {
            ViewModelProvider(this,ViewModelFactory(ItemListUseCase())).get(MainViewModel::class.java)
        }

    }

Note that I do not pass any arguments to .get(MainViewModel::class.java) because our Factory will take care of injecting those arguments into our constructor.

At the end, if you want to avoid the Factory class, you can always use Dagger to inject your dependencies without worrying about the ViewModel Factory class.

Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
1

Try adding this:

private LiveData<Section[]> movies;

And then, change this:

LiveData<List<Movie>> getMovies() {
    if(movies==null)
        movies= movieRepository.getMovies();
    return movies;
}

It worked for me.

Jhon Paul
  • 143
  • 2
  • 7
0

If it helps someone. In my case everything CommonsWare told was right, however, I forgot to inject the AppComponennt.

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        // this line was missing
        ((BaseApplication) getApplication()).getAppComponent().inject(this); 
        super.onCreate(savedInstanceState);
}
Soon Santos
  • 2,107
  • 22
  • 43
0
    In HomeViewModel class need to extend ViewModel class.


public class MovieListViewModel extends ViewModel {
        private MutableLiveData<MovieModel> data;
        private MovieRepository movieModel;

        public MovieListViewModel() {
            movieModel = new MovieRepository();
        }

        public void init() {
            if (this.data != null) {
                // ViewModel is created per Fragment so
                // we know the userId won't change
                return;
            }
            data = movieModel.getMovies();
        }

        public MutableLiveData<MovieModel> getMovies() {
            return this.data;
        }
    }

https://github.com/kamydeep00178/androidMvvm can aslo check full example

Kamal Kakkar
  • 314
  • 1
  • 4
  • 13
0

As it was mentioned in other answers, ViewModelProviders cannot inject objects that you require in ViewModel constructor.

If you need to set up ViewModel quick and ugly without any factories, then you can use Dagger2 that would create getYourViewModel() method in the generated DaggerActivityComponent, while putting those objects into your constructor that you made available with @Provides annotation. Then just add this field to your activity:

@Inject
lateinit var viewmodel: YourViewModel

and inject it in OnCreate() and that's it. NB: Be careful with this approach with heavy and slow ViewModels.

soshial
  • 5,906
  • 6
  • 32
  • 40
-4

Based on this answer, you can do this

class MvpApp : Application(){

    companion object {
        lateinit var application: Application
    }

    override fun onCreate() {
        super.onCreate()
        application = this
    }
}

and then:

class ProfileViewModel: AndroidViewModel(MvpApp.application) {}
kike
  • 4,255
  • 5
  • 23
  • 41