java.lang.ClassCastException: android.support.constraint.Con



我正在做一个项目,我遵循了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)

这是代码回收器视图适配器.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>

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

你关心ConstraintLayoutCardView .更改此行

 <android.support.constraint.ConstraintLayout

android.support.v7.widget.CardView

下行的问题

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

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

取代

<android.support.constraint.ConstraintLayout

<android.support.v7.widget.CardView

还要注意关闭标签

相关内容

  • 没有找到相关文章

最新更新