Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/361.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.lang.ClassCastException:android.support.constraint.ConstraintLayout不能强制转换为android.support.v7.widget.CardView_Java_Android - Fatal编程技术网

java.lang.ClassCastException:android.support.constraint.ConstraintLayout不能强制转换为android.support.v7.widget.CardView

java.lang.ClassCastException:android.support.constraint.ConstraintLayout不能强制转换为android.support.v7.widget.CardView,java,android,Java,Android,我第一次做了一个项目,我在RecycleView上学习了Youtube教程,但当我编译我的项目并在Android Studio上启动应用程序时,我的应用程序崩溃了。我完全按照视频显示的内容进行操作,但有可能是因为某些组件的工作方式不同 这是我得到的输出: java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v7.widget.Card

我第一次做了一个项目,我在RecycleView上学习了Youtube教程,但当我编译我的项目并在Android Studio上启动应用程序时,我的应用程序崩溃了。我完全按照视频显示的内容进行操作,但有可能是因为某些组件的工作方式不同

这是我得到的输出:

java.lang.ClassCastException: android.support.constraint.ConstraintLayout cannot be cast to android.support.v7.widget.CardView
    at e.user.popcorn.RecyclerViewAdapter$MyViewHolder.<init>(RecyclerViewAdapter.java:78)
    at e.user.popcorn.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:33)
    at e.user.popcorn.RecyclerViewAdapter.onCreateViewHolder(RecyclerViewAdapter.java:16)
这是Layout_boxoffice_movie.xml

<?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/movie_id"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="120dp"
android:layout_height="200dp"
android:layout_margin="5dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

    <ImageView
        android:id="@+id/movie_poster"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:scaleType="centerCrop"
        android:background="@color/colorNavBackground"/>

    <TextView
        android:id="@+id/movie_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Movie Title"
        android:gravity="center"
        android:textColor="@color/colorNavBackground"/>

</LinearLayout>


您关心如何将
ConstraintLayout
转换为
CardView
。 换行

 <android.support.constraint.ConstraintLayout
您需要将
ConstraintLayout
替换为
CardView

这将是您更正的代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/movie_id"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="120dp"
android:layout_height="200dp"
android:layout_margin="5dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

    <ImageView
        android:id="@+id/movie_poster"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:scaleType="centerCrop"
        android:background="@color/colorNavBackground"/>

    <TextView
        android:id="@+id/movie_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Movie Title"
        android:gravity="center"
        android:textColor="@color/colorNavBackground"/>

</LinearLayout>
</android.support.v7.widget.CardView>

下线有问题

cardView = (CardView) itemView.findViewById(R.id.movie_id);
根据布局xml文件,您使用的是ConstraintLayout,而在Java文件中使用的是CardView。这才是真正的问题。您需要使用CardView而不是ConstraintLayout

替换

<android.support.constraint.ConstraintLayout

抱歉,这是我第一次发布,我想添加“大家好!”和“谢谢你们的帮助?我希望我提供了足够的元素!”:)共享layout_boxoffice_movie.xml的xml代码在layout_boxoffice_movie.xml中您正在使用ConstraintLayout,但您正试图将此视图作为CardView查找,这就是为什么会出现此错误,如果您共享代码,我可以为您更正此错误。我添加了layout_boxoffice_movie.xml!好的,明白了,我会在下面加上一个答案,如果这个答案能帮助你接受这个问题的话。非常感谢你的帮助@Thunder!如果我没有早点回复,我很抱歉!
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/movie_id"
android:clickable="true"
android:foreground="?android:attr/selectableItemBackground"
android:layout_width="120dp"
android:layout_height="200dp"
android:layout_margin="5dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto">

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorWhite">

    <ImageView
        android:id="@+id/movie_poster"
        android:layout_width="match_parent"
        android:layout_height="170dp"
        android:scaleType="centerCrop"
        android:background="@color/colorNavBackground"/>

    <TextView
        android:id="@+id/movie_title"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="Movie Title"
        android:gravity="center"
        android:textColor="@color/colorNavBackground"/>

</LinearLayout>
</android.support.v7.widget.CardView>
cardView = (CardView) itemView.findViewById(R.id.movie_id);
<android.support.constraint.ConstraintLayout
<android.support.v7.widget.CardView