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

时间:2018-04-11 18:45:44

标签: java android

我正在制作第一个项目,我在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)

这就是代码 RecyclerViewAdapter.java

    package e.user.popcorn;

    import android.content.Context;
    import android.content.Intent;
    import android.media.Image;
    import android.support.v7.widget.CardView;
    import android.support.v7.widget.RecyclerView;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.ImageView;
    import android.widget.TextView;

    import java.util.List;

    public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {

    private Context mContext ;
    private List<movie_page> mData ;


    public RecyclerViewAdapter(Context mContext, List<movie_page> mData) {
        this.mContext = mContext;
        this.mData = mData;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view ;
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        view = mInflater.inflate(R.layout.layout_boxoffice_movie,parent,false);
        return new MyViewHolder(view);
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {

        holder.tv_movie_title.setText(mData.get(position).getTitle());
        holder.img_movie_thumbnail.setImageResource(mData.get(position).getThumbnail());
        holder.cardView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(mContext,SelectedMovie.class);

                // passing data to the book activity
                intent.putExtra("Title",mData.get(position).getTitle());
                intent.putExtra("Description",mData.get(position).getDescription());
                intent.putExtra("Thumbnail",mData.get(position).getThumbnail());
                intent.putExtra("Categorie",mData.get(position).getCategory());
                // start the activity
                mContext.startActivity(intent);

            }
        });
    }

    @Override
    public int getItemCount() {
        return mData.size();
    }

    public static class MyViewHolder extends RecyclerView.ViewHolder {

        TextView tv_movie_title;
        ImageView img_movie_thumbnail;
        CardView cardView ;

        public MyViewHolder(View itemView) {
            super(itemView);

            tv_movie_title = (TextView) itemView.findViewById(R.id.movie_title) ;
            img_movie_thumbnail = (ImageView) itemView.findViewById(R.id.movie_poster);
            cardView = (CardView) itemView.findViewById(R.id.movie_id);
        }
    }
}

还有movie_page.java

package e.user.popcorn;

public class movie_page {

    private String Title;
    private String Category ;
    private String Description ;
    private int Thumbnail ;

    public movie_page() {
    }

    public movie_page(String title, String category, String description, int thumbnail) {
        Title = title;
        Category = category;
        Description = description;
        Thumbnail = thumbnail;
    }


    public String getTitle() {
        return Title;
    }

    public String getCategory() {
        return Category;
    }

    public String getDescription() {
        return Description;
    }

    public int getThumbnail() {
        return Thumbnail;
    }


    public void setTitle(String title) {
        Title = title;
    }

    public void setCategory(String category) {
        Category = category;
    }

    public void setDescription(String description) {
        Description = description;
    }

    public void setThumbnail(int thumbnail) {
        Thumbnail = thumbnail;
    }
}

这是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>

3 个答案:

答案 0 :(得分:0)

您关注<Action xsi:type="ShowTaskpane"> <TaskpaneId>MyPane</TaskpaneId> <SourceLocation resid="myproject.url.base.in" /> </Action> <Action xsi:type="ShowTaskpane"> <TaskpaneId>MyPane</TaskpaneId> <SourceLocation resid="myproject.url.base.out" /> </Action> ConstraintLayout。 改变这一行

CardView

 <android.support.constraint.ConstraintLayout

答案 1 :(得分:0)

  

您需要将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>

答案 2 :(得分:0)

以下问题

cardView = (CardView) itemView.findViewById(R.id.movie_id);

根据您的布局xml文件,您正在使用ConstraintLayout,而您在Java文件中使用CardView。这是真正的问题。您需要使用CardView而不是ConstraintLayout。

替换

<android.support.constraint.ConstraintLayout

使用

<android.support.v7.widget.CardView

还要注意结束标签