Dagger-Hilt @AndroidEntryPoint 不适用于 BottomSheetDialogFragment()
Dagger-Hilt @AndroidEntryPoint is not working with BottomSheetDialogFragment()
我在 BottomSheetDialogFragmet() 中使用 ViewModel,所以我必须用 @AndroidEntryPoint 标记我的 BottomSheet。
@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment() {
private val viewModel: MyAddressesViewModel by viewModels()
......
}
但是当我尝试构建我的项目时,出现了这个错误:
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.
这是我使用 --stacktrace
对 运行 的输出
https://gist.github.com/javlonrahimov/95de968645cace1dfb6e425381f8014b
如果我删除 BottomSheetFragmen() 上方的 @AndroidEntryPoint,它正在构建和安装应用程序。但是当我尝试打开 bottomSheet 时,应用程序崩溃了,因为我将我的存储库注入了 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.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.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 在其他普通 Fragments 上正常工作。
更新:
这是我的视图模型:
@HiltViewModel
class MyAddressesViewModel @Inject constructor(
private val repository: AddressRepository,
private val geocoderRepository: GeocoderRepository
) : ViewModel() {}
更新 2:
现在我正在注释 BottomSheetDialogFragment() 并且它在构建时给我错误。
如果我改变这个
@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment(){
至此
@AndroidEntryPoint
class SearchAddressDialog : Fragment() {
它工作得很好。
我真的可以用@AndroidEntryPoint 注释 BottomSheetDialogFragment() 吗?
更新 3:
您是否在
上添加了 @Inject
注释
AddressRepository
和 GeocoderRepository
class 是构造函数?也许你忘记注释你的 ViewModel class 的依赖对象的构造函数。因此 Hilt/Dagger 无法解析依赖关系树。
如果是这种情况,请用 @Inject
注释所有依赖的 class 构造函数,问题将得到解决。
来自文档:
Hilt only supports fragments that extend androidx.Fragment.
首先,希望您在项目中添加了hilt-android-gradle-plugin
。
其次,dagger-hilt 很久以前就存在一个关于这种继承依赖的问题,但自 2.28 以来它已经被修复。您可以在此 link 中查看更多详细信息。 https://github.com/google/dagger/issues/1910
因此,对于您的情况,不将 @AndroidEntryPoint
添加到 BottomSheetDialogFragment
应该可行,但您是否也尝试过添加这个?它是这样工作的吗?
此外,能否请您分享您的 BottomSheetDialogFragment class?
经过长时间的研究,我无法解决@AndroidEntryPoint 问题。
我想出了这个:我只是将我的 ViewModel 传递给它的构造函数上的 BottomSheetDialogFragment:
class SearchAddressDialog(
private val viewModel: MyAddressesViewModel
) : BottomSheetDialogFragment() {
........
}
我这样打开对话框:
val dialog = SearchAddressDialog(viewModel)
dialog.show(childFragmentManager, SearchAddressDialog.TAG)
我已经使用了当前片段的ViewModel。
@AndroidEntryPoint & @HiltViewModel
完美运行!
这是我的底部 sheet 片段和视图模型。
@AndroidEntryPoint
class BottomSheetFragment : BottomSheetDialogFragment() {
companion object {
const val TAG = "BottomSheetFragment"
}
private val viewModel by viewModels<BottomSheetFragmentViewModel>()
private var _binding: BottomSheetFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,): View {
_binding = BottomSheetFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.test()
}
}
@HiltViewModel
class BottomSheetFragmentViewModel @Inject constructor(
private val localData: LocalRepository,
private val remoteData: RemoteRepository,
) : BaseViewModel() {
fun test() {}
}
最后像这样从另一个片段打开它:
BottomSheetFragment().show(childFragmentManager, BottomSheetFragment.TAG);
注意:使用匕首和刀柄版本 2.41
希望这对您有所帮助!
编码愉快!!
我在 BottomSheetDialogFragmet() 中使用 ViewModel,所以我必须用 @AndroidEntryPoint 标记我的 BottomSheet。
@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment() {
private val viewModel: MyAddressesViewModel by viewModels()
......
}
但是当我尝试构建我的项目时,出现了这个错误:
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.
这是我使用 --stacktrace
对 运行 的输出https://gist.github.com/javlonrahimov/95de968645cace1dfb6e425381f8014b
如果我删除 BottomSheetFragmen() 上方的 @AndroidEntryPoint,它正在构建和安装应用程序。但是当我尝试打开 bottomSheet 时,应用程序崩溃了,因为我将我的存储库注入了 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.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.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 在其他普通 Fragments 上正常工作。
更新:
这是我的视图模型:
@HiltViewModel
class MyAddressesViewModel @Inject constructor(
private val repository: AddressRepository,
private val geocoderRepository: GeocoderRepository
) : ViewModel() {}
更新 2: 现在我正在注释 BottomSheetDialogFragment() 并且它在构建时给我错误。
如果我改变这个
@AndroidEntryPoint
class SearchAddressDialog : BottomSheetDialogFragment(){
至此
@AndroidEntryPoint
class SearchAddressDialog : Fragment() {
它工作得很好。
我真的可以用@AndroidEntryPoint 注释 BottomSheetDialogFragment() 吗?
更新 3:
您是否在
上添加了 @Inject
注释
AddressRepository
和 GeocoderRepository
class 是构造函数?也许你忘记注释你的 ViewModel class 的依赖对象的构造函数。因此 Hilt/Dagger 无法解析依赖关系树。
如果是这种情况,请用 @Inject
注释所有依赖的 class 构造函数,问题将得到解决。
来自文档:
Hilt only supports fragments that extend androidx.Fragment.
首先,希望您在项目中添加了hilt-android-gradle-plugin
。
其次,dagger-hilt 很久以前就存在一个关于这种继承依赖的问题,但自 2.28 以来它已经被修复。您可以在此 link 中查看更多详细信息。 https://github.com/google/dagger/issues/1910
因此,对于您的情况,不将 @AndroidEntryPoint
添加到 BottomSheetDialogFragment
应该可行,但您是否也尝试过添加这个?它是这样工作的吗?
此外,能否请您分享您的 BottomSheetDialogFragment class?
经过长时间的研究,我无法解决@AndroidEntryPoint 问题。 我想出了这个:我只是将我的 ViewModel 传递给它的构造函数上的 BottomSheetDialogFragment:
class SearchAddressDialog(
private val viewModel: MyAddressesViewModel
) : BottomSheetDialogFragment() {
........
}
我这样打开对话框:
val dialog = SearchAddressDialog(viewModel)
dialog.show(childFragmentManager, SearchAddressDialog.TAG)
我已经使用了当前片段的ViewModel。
@AndroidEntryPoint & @HiltViewModel
完美运行!
这是我的底部 sheet 片段和视图模型。
@AndroidEntryPoint
class BottomSheetFragment : BottomSheetDialogFragment() {
companion object {
const val TAG = "BottomSheetFragment"
}
private val viewModel by viewModels<BottomSheetFragmentViewModel>()
private var _binding: BottomSheetFragmentBinding? = null
private val binding get() = _binding!!
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?,): View {
_binding = BottomSheetFragmentBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
viewModel.test()
}
}
@HiltViewModel
class BottomSheetFragmentViewModel @Inject constructor(
private val localData: LocalRepository,
private val remoteData: RemoteRepository,
) : BaseViewModel() {
fun test() {}
}
最后像这样从另一个片段打开它:
BottomSheetFragment().show(childFragmentManager, BottomSheetFragment.TAG);
注意:使用匕首和刀柄版本 2.41
希望这对您有所帮助! 编码愉快!!