Skip to content
Advertisement

android.view.ContextThemeWrapper cannot be cast to android.app.Activity

I’m not a designer but when i got this project i can`t open specifically some screens, i think that they are screens and we only reuse some of the layouts have been created. Anyway can somebody help me? @Override public void onBindViewHolder(@NonNull final ProductsAdapter.ViewHolder holder, final int position) { String imageUrl = ProductsList.get(position).getImage();

JavaScript

and the logcat shows this

JavaScript

Advertisement

Answer

This line is probably the culprit:

JavaScript

The view v passed to the onClick() method is the same view that you assigned the listener to, so v is the same as holder.parentLayot. I don’t know exactly where holder.parentLayot came from, but chances are very good that (in XML) this view (or one of its parents) has an android:theme attribute.

When a view has the android:theme attribute, it doesn’t use its activity’s context directly. Instead, the android framework will “wrap” the activity’s context in a ContextThemeWrapper in order to modify the view’s theme.

To access the activity from this wrapper, you’ll have to “unwrap” it. Try something like this:

JavaScript

Then, you can use this method in your onClick() instead of casting the context directly:

JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement