Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android.view.ContextThemeWrapper无法强制转换为android.app.Activity_Java_Android_Android Studio - Fatal编程技术网

Java android.view.ContextThemeWrapper无法强制转换为android.app.Activity

Java android.view.ContextThemeWrapper无法强制转换为android.app.Activity,java,android,android-studio,Java,Android,Android Studio,我不是一个设计师,但当我得到这个项目时,我无法打开一些特定的屏幕,我认为它们是屏幕,我们只重用已经创建的一些布局。 有人能帮我吗? @凌驾 BindViewHolder上的公共无效(@NonNull final ProductsAdapter.ViewHolder,final int位置){ 字符串imageUrl=ProductsList.get(position.getImage() logcat显示了这一点 java.lang.ClassCastException: android.vie

我不是一个设计师,但当我得到这个项目时,我无法打开一些特定的屏幕,我认为它们是屏幕,我们只重用已经创建的一些布局。 有人能帮我吗? @凌驾 BindViewHolder上的公共无效(@NonNull final ProductsAdapter.ViewHolder,final int位置){ 字符串imageUrl=ProductsList.get(position.getImage()

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 

这条线可能是罪魁祸首:

传递给
onClick()
方法的视图
v
与您分配给侦听器的视图相同,因此
v
holder.parentlayout
相同。我不清楚
holder.parentlayout
的确切来源,但很可能(在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());

Kotlin中的递归解决方案:

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

检查了使用
View.getContext()

的情况,我认为您的错误从这一行开始
Activity Activity=(Activity)v.getContext();
。有什么建议吗?我是新手,您能解释一下我如何识别竞赛包装器吗?我不确定您的意思。您不需要“识别”它;
v.getContext()
将返回一个上下文,它是一个
上下文主题包装器
,也不是一个
活动
。我发布的代码将打开任何这样的上下文,直到它找到一个活动,所以你可以将其粘贴进去,它应该可以工作。很好的解释!我的几个协调人都在使用这个应用程序:Theme这个解决方案非常有用!
Activity activity = unwrap(v.getContext());
fun Context.getActivity(): Activity? {
    return when (this) {
        is Activity -> this
        is ContextWrapper -> this.baseContext.getActivity()
        else -> null
    }
}