使用FirebaseRecyclerViewAdapter投射错误 [英] Cast Error with FirebaseRecyclerViewAdapter
问题描述
我遵循这个例子: https://github.com/firebase/firebaseui-android#using-a-recyclerview
我得到一个奇怪的错误,但:
java.lang.ClassCastException:
android.support.v7.widget.AppCompatTextView无法转换为
android.view.ViewGroup
这是我有在onCreateView:
RecyclerView recycler =(RecyclerView)mView.findViewById(R.id.rvTasks);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerViewAdapter mAdapter =
FirebaseRecyclerViewAdapter< Task,TaskViewHolder>(Task.class,android.R.layout.simple_list_item_1,TaskViewHolder.class,mRef){
@Override
public void populateViewHolder(TaskViewHolder taskViewHolder,Task task){
taskViewHolder.taskText.setText(task.getText());
}
};
recycler.setAdapter(mAdapter);
这是ViewHolder:
私人静态类TaskViewHolder扩展RecyclerView.ViewHolder {
TextView taskText;
public TaskViewHolder(View itemView){
超级(ItemView控件);
taskText =(TextView)itemView.findViewById(android.R.id.text1);
$ b $ p $这是任务类: public class Task {
字符串作者;
字符串文本;
$ b $ public Task(){
}
public Task(String author,String text){
this.author = author;
this.text = text;
}
public String getAuthor(){
return author;
}
public String getText(){
return text;
$ / code $ / pre
$ b $ p 任何想法?提前致谢!
解决方案你应该真的把更多的错误放在你的文章上。这将显示问题在哪里发生。不过,我会在看看 FirebaseRecyclerViewAdapter
的源代码后尝试做出有根据的猜测。
与传递给适配器的布局一致, android.R.layout.simple_list_item_1
。适配器需要一个由 ViewGroup
子类包装的布局,比如 LinearLayout
, FrameLayout
或一些其他类型的布局。 android.R.layout.simple_list_item_1
是这样实现的:
< ; TextView xmlns:android =http://schemas.android.com/apk/res/android
android:id =@ android:id / text1
android:layout_width =match_parent
android:layout_height =wrap_content
android:textAppearance =?android:attr / textAppearanceListItemSmall
android:gravity =center_vertical
android:paddingStart =?android: attr / listPreferredItemPaddingStart
android:paddingEnd =?android:attr / listPreferredItemPaddingEnd
android:minHeight =?android:attr / listPreferredItemHeightSmall/>
正如您所看到的, TextView
是不包裹在布局内。解决这个错误最快捷的方法就是创建你自己的布局,在 FrameLayout
内使用 TextView
:
< FrameLayout xmlns:android =http://schemas.android.com/apk/res/android
android:layout_width =match_parent
android:layout_height =wrap_content>
< TextView
android:id =@ android:id / text1
android:layout_width =match_parent
android:layout_height =wrap_content
android :textAppearance =?android:attr / textAppearanceListItemSmall
android:gravity =center_vertical
android:paddingStart =?android:attr / listPreferredItemPaddingStart
android:paddingEnd =?android: attr / listPreferredItemPadendEnd
android:minHeight =?android:attr / listPreferredItemHeightSmall/>
< / FrameLayout>
然后,您将创建的布局传递给适配器。让我们把它称为 my_simple_list_item_1.xml
,你可以把它作为 R.layout.my_simple_list_item_1
传递给适配器。
So I have a RecycleView that I'm to fill with a FirebaseRecyclerViewAdapter.
I'm following this example: https://github.com/firebase/firebaseui-android#using-a-recyclerview
I'm getting a strange error, though:
java.lang.ClassCastException:
android.support.v7.widget.AppCompatTextView cannot be cast to
android.view.ViewGroup
This is what I have in onCreateView:
RecyclerView recycler = (RecyclerView) mView.findViewById(R.id.rvTasks);
recycler.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerViewAdapter mAdapter =
new FirebaseRecyclerViewAdapter<Task, TaskViewHolder>(Task.class, android.R.layout.simple_list_item_1, TaskViewHolder.class, mRef) {
@Override
public void populateViewHolder(TaskViewHolder taskViewHolder, Task task) {
taskViewHolder.taskText.setText(task.getText());
}
};
recycler.setAdapter(mAdapter);
This is the ViewHolder:
private static class TaskViewHolder extends RecyclerView.ViewHolder {
TextView taskText;
public TaskViewHolder(View itemView) {
super(itemView);
taskText = (TextView)itemView.findViewById(android.R.id.text1);
}
}
This is the Task class:
public class Task {
String author;
String text;
public Task() {
}
public Task(String author, String text) {
this.author = author;
this.text = text;
}
public String getAuthor() {
return author;
}
public String getText() {
return text;
}
}
Any ideas? Thanks in advance!
解决方案 You should have really placed more of the error on your post. It would have shown where the issue was happening. However, I will try to make an educated guess after looking at he source for FirebaseRecyclerViewAdapter
.
The issue appears to be with the layout that you passes to the adapter, android.R.layout.simple_list_item_1
. The adapter expects a layout that is wrapped by a subclass of ViewGroup
, such as a LinearLayout
, FrameLayout
or some other type of Layout. android.R.layout.simple_list_item_1
is implemented like this:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
As you can see, the TextView
is not wrapped inside of a Layout. The quickest way to fix the error would be to create your own layout, with a TextView
inside of a FrameLayout
like so:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center_vertical"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
</FrameLayout>
You would then pass the layout you created to the adapter. Lets call it my_simple_list_item_1.xml
, which you would pass to the adapter as R.layout.my_simple_list_item_1
.
这篇关于使用FirebaseRecyclerViewAdapter投射错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!