Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();

    holder.itemName.setText(ProductsList.get(position).getName());
    Picasso.with(context).load(imageUrl).into(holder.itemImage);

    holder.parentLayot.setOnClickListener(new View.OnClickListener() {
        public String currentActivity;

        @Override
        public void onClick(View v) {
            Activity activity = (Activity) v.getContext();
            activity.runOnUiThread(bd);
            String activityName = activity.getClass().getSimpleName();
            // Toast.makeText(activity,ProductsList.get(position).getId(),Toast.LENGTH_SHORT).show();

            if (activityName.equals("Product_View")){
                // Toast.makeText(activity,"Click and pass",Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(activity, Second_Product_View.class);
                intent.putExtra("item_name", ProductsList.get(position).getName());
                intent.putExtra("item_id", ProductsList.get(position).getId());
                intent.putExtra("item_category_name", ProductsList.get(position).getName());
                intent.putExtra("item_category_id", ProductsList.get(position).getId());

                activity.startActivity(intent);
            } else if(activityName.equals("Second_Product_View")){
                Intent intent = new Intent(activity, Third_Product_View.class);

                intent.putExtra("item_category_name", activity.getIntent().getStringExtra("item_category_name"));
                intent.putExtra("item_category_id", activity.getIntent().getStringExtra("item_category_id"));

                intent.putExtra("item_subcategory_name", ProductsList.get(position).getName());
                intent.putExtra("item_subcategory_id", ProductsList.get(position).getId());

                intent.putExtra("item_name", ProductsList.get(position).getName());
                intent.putExtra("item_id", ProductsList.get(position).getId());

                activity.startActivity(intent);
            } else if(activityName.equals("Third_Product_View")){
                Intent intent = new Intent(activity, Fourth_Product_View.class);

                intent.putExtra("item_category_name", activity.getIntent().getStringExtra("item_category_name"));
                intent.putExtra("item_category_id", activity.getIntent().getStringExtra("item_category_id"));

                intent.putExtra("item_subcategory_name", activity.getIntent().getStringExtra("item_subcategory_name"));
                intent.putExtra("item_subcategory_id", activity.getIntent().getStringExtra("item_subcategory_id"));

                intent.putExtra("item_section_name", ProductsList.get(position).getName());
                intent.putExtra("item_section_id", ProductsList.get(position).getId());

                intent.putExtra("item_name", ProductsList.get(position).getName());
                intent.putExtra("item_id", ProductsList.get(position).getId());
                activity.startActivity(intent);
            } else if(activityName.equals("Fourth_Product_View")){
                holder.downloadManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);
                String file = activity.getString(R.string.base_url) + "files/example-" + ProductsList.get(position).getId() + ".pdf";
                Uri uri = Uri.parse(file);
                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                Long reference = holder.downloadManager.enqueue(request);
            } else if (activityName.equals("Formation_View")){
                Intent intent = new Intent(activity, Second_Formation_View.class);
                intent.putExtra("item_name", ProductsList.get(position).getName());
                intent.putExtra("item_id", ProductsList.get(position).getId());
                intent.putExtra("item_category_name", ProductsList.get(position).getName());
                intent.putExtra("item_category_id", ProductsList.get(position).getId());

                activity.startActivity(intent);
            } else if(activityName.equals("Second_Formation_View")){
                Intent intent = new Intent(activity, Third_Formation_View.class);

                intent.putExtra("item_category_name", activity.getIntent().getStringExtra("item_category_name"));
                intent.putExtra("item_category_id", activity.getIntent().getStringExtra("item_category_id"));

                intent.putExtra("item_subcategory_name", ProductsList.get(position).getName());
                intent.putExtra("item_subcategory_id", ProductsList.get(position).getId());

                intent.putExtra("item_name", ProductsList.get(position).getName());
                intent.putExtra("item_id", ProductsList.get(position).getId());

                activity.startActivity(intent);
            } else if(activityName.equals("Third_Formation_View")){
                holder.downloadManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);
                String file = activity.getString(R.string.base_url) + "files/example-" + ProductsList.get(position).getId() + ".pdf";
                Uri uri = Uri.parse(file);
                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                Long reference = holder.downloadManager.enqueue(request);
            } else if (activityName.equals("Tools_View")){
                Intent intent = new Intent(activity, Second_Tools_View.class);
                intent.putExtra("item_name", ProductsList.get(position).getName());
                intent.putExtra("item_id", ProductsList.get(position).getId());
                intent.putExtra("item_category_name", ProductsList.get(position).getName());
                intent.putExtra("item_category_id", ProductsList.get(position).getId());

                activity.startActivity(intent);
            } else if(activityName.equals("Second_Tools_View")){
                holder.downloadManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);
                String file = activity.getString(R.string.base_url) + "files/example-" + ProductsList.get(position).getId() + ".pdf";
                Uri uri = Uri.parse(file);
                DownloadManager.Request request = new DownloadManager.Request(uri);
                request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                Long reference = holder.downloadManager.enqueue(request);
            }

        }
    });
} 

and the logcat shows this

java.lang.ClassCastException: android.view.ContextThemeWrapper cannot be cast to android.app.Activity

    at android.view.View.performClick(View.java:6256)
    at android.view.View$PerformClick.run(View.java:24701)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at 
like image 296
Mauricio Quirvan Campuzano Avatar asked Aug 01 '18 18:08

Mauricio Quirvan Campuzano


2 Answers

This line is probably the culprit:

Activity activity = (Activity) v.getContext();

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:

private static Activity unwrap(Context context) {
    while (!(context instanceof Activity) && context instanceof ContextWrapper) {
        context = ((ContextWrapper) context).getBaseContext();
    }

    return (Activity) context;
}

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

Activity activity = unwrap(v.getContext());
like image 170
Ben P. Avatar answered Nov 19 '22 19:11

Ben P.


Recursive solution in Kotlin:

fun Context.getActivity(): Activity? {
    return when (this) {
        is Activity -> this
        is ContextWrapper -> this.baseContext.getActivity()
        else -> null
    }
}

Checked with case of using View.getContext().

like image 21
artem Avatar answered Nov 19 '22 17:11

artem