java - 为什么回收站 View 不显示任何错误为 : E/RecyclerView: No adapter attached; skipping layout 的帖子

标签 java android android-recyclerview google-cloud-firestore

我正在使用多个查询来填充 ReceyclerView,但是我不断收到错误 E/RecyclerView:未连接适配器;跳过布局问题不是我没有在OnCreate中调用RecyclerView,因为所有代码都在OnCreate

我已经检查过我的 RecyclerView 是我的 OnCreate,所以事实并非如此。

更新:OnCreate:

 @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        super.onCreateView(inflater, container, savedInstanceState);

        View rootView = inflater.inflate(R.layout.fragment, container, false);
        final List<String> followingList = new ArrayList<>();
        final List<Post> postList = new ArrayList<>();
        final FollowerAdapter followerAdapter = new FollowerAdapter(postList);
        final RecyclerView recyclerView = rootView.findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new LinearLayoutManager(this.getActivity(), RecyclerView.VERTICAL, false));
        final LinearLayoutManager linearLayoutManager = ((LinearLayoutManager) recyclerView.getLayoutManager());
        recyclerView.setAdapter(followerAdapter);

        Query first = userFollowingReference;//TODO Realtime updates
        first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
            @Override
            public void onComplete(@NonNull final Task<QuerySnapshot> task) {
                if (task.isSuccessful()) {
                    for (DocumentSnapshot document : task.getResult()) {
                        String id = document.getId();
                        followingList.add(id);
                    }
                    followerAdapter.notifyDataSetChanged();
                    if(!followingList.isEmpty()) {

                        Query second = rootRef.collection("posts/" + followingList.get(0) + "/userPosts")
                                .orderBy("date", Query.Direction.ASCENDING)
                                .limit(3);
                        second.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                            @Override
                            public void onComplete(@NonNull final Task<QuerySnapshot> task) {
                                if(task.isSuccessful()) {
                                    for(DocumentSnapshot document : task.getResult()){
                                        Post post = document.toObject(Post.class);
                                        postList.add(post);
                                    }
                                    followerAdapter.notifyDataSetChanged();
                                    if(!postList.isEmpty()) {
                                        lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                                        RecyclerView.OnScrollListener onScrollListener = new RecyclerView.OnScrollListener() {
                                            @Override
                                            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {
                                                super.onScrollStateChanged(recyclerView, newState);
                                                if (newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
                                                    isScrolling = true;
                                                }
                                            }

                                            @Override
                                            public void onScrolled(final RecyclerView recyclerView, int dx, int dy) {
                                                super.onScrolled(recyclerView, dx, dy);
                                                lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);
                                                int firstVisibleItemPosition = linearLayoutManager.findFirstVisibleItemPosition();
                                                int visibleItemCount = linearLayoutManager.getChildCount();
                                                int totalItemCount = linearLayoutManager.getItemCount();

                                                if (isScrolling && (firstVisibleItemPosition + visibleItemCount == totalItemCount) && !isLastItemReached) {
                                                    isScrolling = false;

                                                    Query third = rootRef.collection("posts/" + followingList.get(task.getResult().size() - 1) + "/userPosts")
                                                            .orderBy("date", Query.Direction.ASCENDING)
                                                            .startAfter(lastVisible).limit(3);
                                                    third.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
                                                        @Override
                                                        public void onComplete(@NonNull Task<QuerySnapshot> t) {
                                                            if (t.isSuccessful()) {
                                                                for (DocumentSnapshot d : t.getResult()) {
                                                                    Post post = d.toObject(Post.class);
                                                                    postList.add(post);
                                                                }
                                                                followerAdapter.notifyDataSetChanged();
                                                                lastVisible = task.getResult().getDocuments().get(task.getResult().size() - 1);

                                                                if (t.getResult().size() < 3) {
                                                                    isLastItemReached = true;
                                                                }
                                                            }
                                                        }
                                                    });
                                                }
                                            }
                                        };

                                        recyclerView.addOnScrollListener(onScrollListener);
                                    }
                                }

                            }
                        });
                    }
                }
            }
        });


        return rootView;

    }

适配器:

class FollowerAdapter extends RecyclerView.Adapter<PostViewHolder> {
    private List<Post> list;

    FollowerAdapter(List<Post> list) {
        this.list = list;
    }

    @NonNull
    @Override
    public PostViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.card_view_layout, parent, false);
        return new PostViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PostViewHolder holder, int position) {
        Post post = list.get(position);
        holder.setPost(post);
    }

    @Override
    public int getItemCount() {
        return list.size();
    }


}

当我运行应用程序时,RecyclerView 为空,并且收到错误:

E/RecyclerView: No adapter attached; skipping layout

此外,通过调试,我发现 followingList 已正确填充,就像保存帖子的 list 一样。看起来好像 RecyclerView 被完全跳过。

编辑:这是我的 Firestore 布局(如果有帮助的话)

Firestore-root
   |
   --- users (collection)
   |     |
   |     --- uid (documents)
   |          |
   |          --- name: "User Name"
   |          |
   |          --- email: "email@email.com"
   |
   --- following (collection)
   |      |
   |      --- uid (document)
   |           |
   |           --- userFollowing (collection)
   |                 |
   |                 --- uid (documents)
   |                 |
   |                 --- uid (documents)
   |
   --- posts (collection)
         |
         --- uid (documents)
              |
              --- userPosts (collection)
                    |
                    --- postId (documents)
                    |
                    --- postId (documents)

感谢大家迄今为止的帮助!

更新: 我现在可以看到几篇文章,但在滚动到 RecyclerView 底部后,我得到 java.lang.IndexOutOfBoundsException 下面是我的 Logcat:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.android, 
    java.lang.IndexOutOfBoundsException: Invalid index 2, size is 1
        at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
        at java.util.ArrayList.get(ArrayList.java:308)
        at com.example.android.Fragments.FollowingFragment$1$1$1.onScrolled(FollowingFragment.java:104)
        at androidx.recyclerview.widget.RecyclerView.dispatchOnScrolled(RecyclerView.java:5077)
        at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5241)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
        at android.view.Choreographer.doCallbacks(Choreographer.java:580)
        at android.view.Choreographer.doFrame(Choreographer.java:549)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:135)
        at android.app.ActivityThread.main(ActivityThread.java:5343)
        at java.lang.reflect.Method.invoke(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:372)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)

最佳答案

它似乎在初始布局过程中没有在 RecyclerView 上找到任何数据。您应该将适配器设置移至 OnCompleteListener 之外,然后从其中更新数据:

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){

    // Your set up/initialization

    recyclerView.setAdapter(yourAdapter);

    first.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
        @Override
        public void onComplete(@NonNull final Task<QuerySnapshot> task){
            // Update your adapter and notifyDataSetChanged when needed here
        }
    }
}

关于java - 为什么回收站 View 不显示任何错误为 : E/RecyclerView: No adapter attached; skipping layout 的帖子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56895583/

相关文章:

java - Flutter Android构建失败

javax.crypto 受导出管制;那是什么意思?

java - 需要高性能 Scala/Java 集合

android - PinnedHeaderListView 示例

Android market 应用设备国家问题

android - 应用程序 :stackFromEnd for RecyclerView is not working in xml?

Java Mission Control 代码分析器为空

android - 记录 HLS block 请求

android - ScrollView android时,项目动画在recyclerview中停止

android - 在 RecyclerView 项目中处置