gpt4 book ai didi

java - ClassCastException 用 Google 示例代码 : how should my my_text_view. xml 文件看一下?

转载 作者:行者123 更新时间:2023-12-02 01:22:13 26 4
gpt4 key购买 nike

我一直在尝试学习如何使用 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 文件的 Root View 是 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);

I would recommend that you go with second option if you are planning to modify this later on as you can't add more items to viewholder if you go with first solution. Otherwise it would crash again.

关于java - ClassCastException 用 Google 示例代码 : how should my my_text_view. xml 文件看一下?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57484079/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com