Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 413 Vote(s) - 3.49 Average
  • 1
  • 2
  • 3
  • 4
  • 5
dagger.hilt.android.internal.managers.ViewComponentManager$FragmentContextWrapper cannot be cast to android.app.Activity

#1
I'm using a third-party library that has some custom view implementations. In the implementation, they call `Utils.showSoftKeyboard(context as Activity)`. This, along with using the fragment as an @AndroidEntryPoint causes the following exception:

`java.lang.ClassCastException: dagger.hilt.android.internal.managers.ViewComponentManager$FragmentContextWrapper cannot be cast to android.app.Activity`.

Is there any way to fix this without altering the third-party library?
Reply

#2
If the third-party library has an issue tracker, it's a good idea to raise an issue about this. There is no guarantee that the `context` of a `View` is an `Activity`: the same problem can be caused just as easily by an `android:theme` attribute in the custom view or any ancestor.

If you don't inflate any `@AndroidEntryPoint` views in your fragment, you can work around this by getting a `LayoutInflater` directly from the activity context.

```
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val baseInflater = LayoutInflater.from(requireActivity()) // NOT context
// ...
}
Reply

#3
Without Hilt Dependencies we can access parent activity as below

(context as ContextWrapper).baseContext
Reply

#4
It might be late for your problem but here's a solution which might help others having the same problem.
You basically should check the context type before trying to cast it.

val mContext = if (context is ViewComponentManager.FragmentContextWrapper)
context.baseContext
else
context


Inspired by [this][1] answer.


[1]:

[To see links please register here]

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through