0

I've just got back to the Java&Android world. In my search for a good project starters that will leverage compilation time DI and MvvM, I found these two:

  1. Writing Testable Android MVVM
  2. Countries - A sample Android app

Now to my problem... I'd like to make a base activity looks like this (keeping the important parts):

public abstract class ViewModelActivity<VM extends IViewModel> extends AppCompatActivity {

private ActivityComponent activityComponent;
@Inject
private VM viewModel;

protected void inject(AppComponent appComponent) {
    appComponent.inject(this);
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    AppComponent appComponent = ((MvvmApplication) getApplication()).getAppComponent();
    inject(appComponent);

    activityComponent =
            DaggerActivityComponent.builder()
            .appComponent(appComponent)
            .activityModule(new ActivityModule(this))
            .build();

    ViewModel.State savedViewModelState = null;
    if (savedInstanceState != null) {
        savedViewModelState = savedInstanceState.getParcelable(EXTRA_VIEW_MODEL_STATE);
    }
    viewModel = createViewModel(savedViewModelState);
}

}

And so my AppComponent should look like:

@AppScope
@Component(modules = {
    AppContextModule.class,
    AppModule.class,
    NetworkModule.class,
    GsonModule.class
})
public interface MyAppComponent extends AppComponent {

    void inject(DashboardActivity baseActivity);

    Picasso picasso();

}

Where AppComponent is:

@AppScope
public interface AppComponent {

    Context appContext();

    void inject(ViewModelActivity viewModelActivity);

}

And finally the concrete Activity is:

public class DashboardActivity extends ViewModelActivity<DashboardViewModel> {}

Now I keep getting the following error:

Error:(29, 10) error: activities.dashboard.DashboardViewModel cannot be 
provided without an @Inject constructor or from an @Provides- or @Produces-
annotated method. This type supports members injection but cannot be 
implicitly provided.
activities.dashboard.DashboardViewModel is injected at
mvvm.activity.ViewModelActivity.viewModel
activities.dashboard.DashboardActivity is injected at
app.inject(baseActivity)

What am I missing?

Thanks!

Tomer
  • 4,382
  • 5
  • 38
  • 48
  • 2
    The error says that this class ``activities.dashboard.DashboardViewModel`` has to either have an annotated constructor with ``@Inject``, or that you need to have a ``@Provides`` method in one of your modules that provides said class. So have you done any of these? – Fred Jul 03 '17 at 12:56
  • Possible duplicate of [How do I fix Dagger 2 error '... cannot be provided \[...\]'?](https://stackoverflow.com/questions/44912080/how-do-i-fix-dagger-2-error-cannot-be-provided) – David Medenjak Jul 04 '17 at 18:44
  • In `DashboardViewModel`'s constructor function, add @Inject on top. That should fix it. – Rohan Kandwal Feb 04 '18 at 05:47

1 Answers1

0

To use MVVM with dagger check out this sample code from google

It really did it very well and it uses the latest trends in dagger as well, like defining submodules and using new AndroidInjection for injecting activities and fragments. It also uses a custom factory for creating your view model that takes care of injecting the constructor fields.

Thracian
  • 43,021
  • 16
  • 133
  • 222
Alireza Ahmadi
  • 5,122
  • 7
  • 40
  • 67