android.view.ContextThemeWrapper 不能转换为 android.app.Activity [英] android.view.ContextThemeWrapper cannot be cast to android.app.Activity

查看:35
本文介绍了android.view.ContextThemeWrapper 不能转换为 android.app.Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不是设计师,但是当我得到这个项目时,我无法打开一些屏幕,我认为它们是屏幕,我们只重用了一些已经创建的布局.无论如何有人可以帮助我吗?@覆盖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 void onClick(View v) {活动活动=(活动)v.getContext();活动.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,"点击并通过",Toast.LENGTH_SHORT).show();意图意图=新意图(活动,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());活动.开始活动(意图);} else if(activityName.equals("Second_Product_View")){意图意图=新意图(活动,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());活动.开始活动(意图);} else if(activityName.equals("Third_Product_View")){意图意图 = 新意图(活动,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());活动.开始活动(意图);} else if(activityName.equals("Fourth_Product_View")){holder.downloadManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);字符串文件 = 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);长引用 = holder.downloadManager.enqueue(request);} else if (activityName.equals("Formation_View")){意图意图=新意图(活动,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());活动.开始活动(意图);} else if(activityName.equals("Second_Formation_View")){意图意图=新意图(活动,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());活动.开始活动(意图);} else if(activityName.equals("Third_Formation_View")){holder.downloadManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);字符串文件 = 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);长引用 = holder.downloadManager.enqueue(request);} else if (activityName.equals("Tools_View")){意图意图=新意图(活动,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());活动.开始活动(意图);} else if(activityName.equals("Second_Tools_View")){holder.downloadManager = (DownloadManager)activity.getSystemService(Context.DOWNLOAD_SERVICE);字符串文件 = 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);长引用 = holder.downloadManager.enqueue(request);}}});}

并且 logcat 显示了这一点

java.lang.ClassCastException: android.view.ContextThemeWrapper 不能转换为 android.app.Activity在 android.view.View.performClick(View.java:6256)在 android.view.View$PerformClick.run(View.java:24701)在 android.os.Handler.handleCallback(Handler.java:789)在 android.os.Handler.dispatchMessage(Handler.java:98)在 android.os.Looper.loop(Looper.java:164)在 android.app.ActivityThread.main(ActivityThread.java:6541)在 java.lang.reflect.Method.invoke(Native Method)在

解决方案

这行可能是罪魁祸首:

<块引用>

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

传递给 onClick() 方法的视图 v 与您分配侦听器的视图相同,因此 v 是相同的作为 holder.parentLayot.我不知道 holder.parentLayot 的确切来源,但很有可能(在 XML 中)这个视图(或其父视图之一)有一个 android:theme 属性.

当视图具有 android:theme 属性时,它不会直接使用其活动的上下文.相反,Android 框架会将 Activity 的上下文包装"在 ContextThemeWrapper 中,以便修改视图的主题.

要从此包装器访问活动,您必须解开"它.尝试这样的事情:

private static Activity unwrap(Context context) {而(!(活动的上下文实例)&& ContextWrapper的上下文实例){上下文 = ((ContextWrapper) 上下文).getBaseContext();}返回(活动)上下文;}

然后,您可以在 onClick() 中使用此方法,而不是直接转换上下文:

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

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 

解决方案

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

这篇关于android.view.ContextThemeWrapper 不能转换为 android.app.Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆