java - 带有 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
解决方案
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);
如果您打算稍后修改此选项,我建议您使用第二个选项,因为如果您使用第一个解决方案,您将无法向查看器添加更多项目。否则它会再次崩溃。
推荐阅读
- rest - REST API 一起设计两个资源
- sql - 如何在 SQL 中将 varchar 列拆分为多个值?
- azure - 使用 Azure AD 身份验证将 Azure VM 隐藏在代理后面
- ios - 我的 PDF 中的文本没有出现在 iOS 的打印预览中是否有原因?
- nlp - 在 R text2vec 包中 - 如何将 LDA 模型生成的主题分配给相关文档
- machine-learning - Batchnorm 中的错误反向传播更新
- python - 如何使用“False”将布尔掩码的大小加倍 - Tensorflow 中的元素
- php - 如何使用 mysqli prepare 语句使用电子邮件或用户名(不是 PDO)登录?
- php - 问:如何在结帐中添加自定义表单字段?
- webpack - 使用 Laravel Mix 时如何包含 webpack 插件?