首页 > 解决方案 > 带有 Google 示例代码的 ClassCastException:我的 my_text_view.xml 文件应该是什么样子?

问题描述

我一直在尝试学习如何使用,但我一直在使用该方法RecyclerView时遇到错误。onCreateViewHolder我怀疑我my_text_view.xml的格式不正确。Google 的文档没有提供示例 XML 文件供我使用。

这是适配器类(直接取自 Google 的教程),以及它下面的 XML 文件:

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
    private String[] mDataset;

    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class MyViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView textView;
        public MyViewHolder(TextView v) {
            super(v);
            textView = v;
        }
    }

    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter(String[] myDataset) {
        mDataset = myDataset;
    }

    @Override
    public MyAdapter.MyViewHolder onCreateViewHolder(ViewGroup parent,
                                                     int viewType) {
        // create a new view
        TextView v = (TextView) LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false); //this line throws a ClassCastException
        MyViewHolder vh = new MyViewHolder(v);
        return vh;
    }

    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.textView.setText(mDataset[position]);

    }

    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return mDataset.length;
    }
}

和内容my_text_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />
</LinearLayout>

我收到此错误:

java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

标签: javaandroid

解决方案


XML 文件的根视图是LinearLayout,但代码正在对其进行膨胀,然后尝试将其转换为 TextView。

您可以从 xml 中删除 LinearLayout,使其看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
  <TextView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="TextView" />

或者,或者,

您可以删除类型转换onCreateViewHolder

 View v =  (LayoutInflater.from(parent.getContext())
                .inflate(R.layout.my_text_view, parent, false)); 
        MyViewHolder vh = new MyViewHolder(v);

在您的 ViewHolder 中:

   textView = v.findViewById(R.id.text);

如果您打算稍后修改此选项,我建议您使用第二个选项,因为如果您使用第一个解决方案,您将无法向查看器添加更多项目。否则它会再次崩溃。


推荐阅读