Cast Error with FirebaseRecyclerViewAdapter

2019-07-13 06:11发布

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!

1条回答
等我变得足够好
2楼-- · 2019-07-13 06:29

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.

查看更多
登录 后发表回答