-2

I'm trying out a new architecture with multi-module and DI though Hilt, I have the following modules:

  • app: Contains MainActivity (which does nothing except holding fragment)
  • featureHome: Contains HomeFragment and HomeViewModel

When I start the app I'm getting java.lang.RuntimeException: Cannot create an instance of class HomeViewModel

App module

@AndroidEntryPoint
class MainActivity: AppCompatActivity()

FeatureHome

class HomeFragment: Fragment() {
    private val viewModel: HomeViewModel by viewModels()
    ...
}
@HiltViewModel
class HomeViewModel @Inject constructor(): ViewModel {
    ....
}

I'm not sure what to do to resolve this. Should my HomeFragment also have @AndroidEntryPoint ?

7

1 Answer 1

0

Your Fragment has to be marked with the annotation @AndroidEntryPoint in order to tell Hilt to inject instances in your Fragment. (documentation)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.