android.view.ContextThemeWrapper 无法转换为 android.app.Activity

Posted

技术标签:

【中文标题】android.view.ContextThemeWrapper 无法转换为 android.app.Activity【英文标题】:android.view.ContextThemeWrapper cannot be cast to android.app.Activity 【发布时间】:2019-01-09 10:19:53 【问题描述】:

我不是设计师,但是当我得到这个项目时,我无法专门打开一些屏幕,我认为它们是屏幕,我们只重用了一些已创建的布局。 无论如何,有人可以帮助我吗? @覆盖 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);
            

        
    );
 

logcat 显示了这一点

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 

【问题讨论】:

我认为你的错误从Activity activity = (Activity) v.getContext();这一行开始。 有什么建议吗? 【参考方案1】:

这行可能是罪魁祸首:

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

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

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

要从此包装器访问活动,您必须“解包”它。试试这样的:

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

    return (Activity) context;

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

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

【讨论】:

我是新手,你能解释一下我如何识别 tHE Con​​testWrapper 吗?请 我不确定你的意思。你不需要“识别”它; v.getContext() 正在返回一个 ContextThemeWrapper 而不是 Activity 的上下文。我发布的代码将解开任何此类上下文,直到它找到一个活动,所以你可以将它粘贴进去,它应该可以工作。 很好的解释!,我的几个 coordinatorLayouts 正在使用 app:theme 解决方法很有帮助!【参考方案2】:

Kotlin 中的递归解决方案:

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

检查使用View.getContext()的情况。

【讨论】:

【参考方案3】:

简单地使用上下文就可以了

FragmentManager fragmentManager = ((AppCompatActivity) context).getSupportFragmentManager();

【讨论】:

以上是关于android.view.ContextThemeWrapper 无法转换为 android.app.Activity的主要内容,如果未能解决你的问题,请参考以下文章