Home > database >  Dagger-Hilt @AndroidEntryPoint is not working with BottomSheetDialogFragment()
Dagger-Hilt @AndroidEntryPoint is not working with BottomSheetDialogFragment()

Time:09-30

I am using ViewModel inside BottomSheetDialogFragmet() so I have to mark my BottomSheet with @AndroidEntryPoint.

@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment() {

    private val viewModel: MyAddressesViewModel by viewModels()
    
    ......
}

But when I tried to build my project it is giving me this error:

Execution failed for task ':app:kaptDebugKotlin'.
> A failure occurred while executing 
org.jetbrains.kotlin.gradle.internal.KaptWithoutKotlincTask$KaptExecutionWorkAction
> java.lang.reflect.InvocationTargetException (no error message)

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option 
to get more log output. Run with --scan to get full insights.

And here is my output for run with --stacktrace

https://gist.github.com/javlonrahimov/95de968645cace1dfb6e425381f8014b

If I delete the @AndroidEntryPoint above my BottomSheetFragmen() it is building and installing the app. But when I try to open the bottomSheet the app is crashing as I injected my repositories to the ViewModel:

java.lang.RuntimeException: Cannot create an instance of class uz.unical.other.ui.my_addresses.view_model.MyAddressesViewModel
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:221)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278)
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185)
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54)
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41)
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.getViewModel(SearchAddressDialog.kt:26)
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.onResume(SearchAddressDialog.kt:86)
    at androidx.fragment.app.Fragment.performResume(Fragment.java:3039)
    at androidx.fragment.app.FragmentStateManager.resume(FragmentStateManager.java:607)
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:306)
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189)
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100)
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002)
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:246)
    at android.app.ActivityThread.main(ActivityThread.java:8528)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
 Caused by: java.lang.InstantiationException: java.lang.Class<uz.unical.other.ui.my_addresses.view_model.MyAddressesViewModel> has no zero argument constructor
    at java.lang.Class.newInstance(Native Method)
    at androidx.lifecycle.ViewModelProvider$NewInstanceFactory.create(ViewModelProvider.java:219)
    at androidx.lifecycle.ViewModelProvider$AndroidViewModelFactory.create(ViewModelProvider.java:278) 
    at androidx.lifecycle.SavedStateViewModelFactory.create(SavedStateViewModelFactory.java:112) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:185) 
    at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:150) 
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:54) 
    at androidx.lifecycle.ViewModelLazy.getValue(ViewModelProvider.kt:41) 
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.getViewModel(SearchAddressDialog.kt:26) 
    at uz.unical.other.ui.my_addresses.dialogs.SearchAddressDialog.onResume(SearchAddressDialog.kt:86) 
    at androidx.fragment.app.Fragment.performResume(Fragment.java:3039) 
    at androidx.fragment.app.FragmentStateManager.resume(FragmentStateManager.java:607) 
    at androidx.fragment.app.FragmentStateManager.moveToExpectedState(FragmentStateManager.java:306) 
    at androidx.fragment.app.FragmentManager.executeOpsTogether(FragmentManager.java:2189) 
    at androidx.fragment.app.FragmentManager.removeRedundantOperationsAndExecute(FragmentManager.java:2100) 
    at androidx.fragment.app.FragmentManager.execPendingActions(FragmentManager.java:2002) 
    at androidx.fragment.app.FragmentManager$5.run(FragmentManager.java:524) 
    at android.os.Handler.handleCallback(Handler.java:938) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:246) 
    at android.app.ActivityThread.main(ActivityThread.java:8528) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:602) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130

@AndroidEntryPoint is working without errors on other ordinary Fragments.

UPDATE:

Here is my view model:

@HiltViewModel
class MyAddressesViewModel @Inject constructor(
   private val repository: AddressRepository,
   private val geocoderRepository: GeocoderRepository
) : ViewModel() {}

UPDATE 2: Right now I am annotating BottomSheetDialogFragment() and it is giving me errors while building.

And If I change this

@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment(){

to this

@AndroidEntryPoint
class SearchAddressDialog : Fragment() {

it is working perfectly fine.

Can I actually annotate BottomSheetDialogFragment() with @AndroidEntryPoint?

CodePudding user response:

Did you add @Inject annotation on
AddressRepository and GeocoderRepository classes constructors? maybe you forget to annotate the constructor of the dependent object of your ViewModel class. because of it Hilt/Dagger is unable to resolve the dependency tree.

If this is the case, annotate all dependent class constructors with @Inject the problem will be resolve.

CodePudding user response:

From the documentation:

Hilt only supports fragments that extend androidx.Fragment.

  • Related