简体   繁体   English

无法为child()中的参数“ pathString”传递null

[英]Can't pass null for argument 'pathString' in child()

I'm currently trying to read the contents from my Firebase Database into a RecyclerView but I want to load them from the last node to first node. 我目前正在尝试将Firebase数据库中的内容读取到RecyclerView但是我想将它们从最后一个节点加载到第一个节点。 So I decided to place the keys into an array then try to read from end of the array to the front, but when I try to access the key array I am presented with Can't pass null for argument 'pathString' in child() error. 因此,我决定将键放入数组中,然后尝试从数组的末尾读取到前端,但是当我尝试访问键数组时,我Can't pass null for argument 'pathString' in child()错误。

Below is the method I am using to load the data: 以下是我用来加载数据的方法:

@Override
public void onStart() {
    super.onStart();

FirebaseRecyclerAdapter<UpcomingList, UpcomingViewHolder> firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<UpcomingList, UpcomingViewHolder>(
            UpcomingList.class,
            R.layout.upcoming_event_row,
            UpcomingViewHolder.class,
            mDatabase
    ) {
        @Override
        protected void populateViewHolder(final UpcomingViewHolder viewHolder, UpcomingList model, int position) {

            /*String CurrentString = date.toString().trim();
            StringTokenizer tokens = new StringTokenizer(CurrentString, "/");
            final String first = tokens.nextToken();
            final String second = tokens.nextToken();*/

            //final String post_key = getRef(position).getKey();
            mDatabase.addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    int i = 0;
                    for (DataSnapshot d : dataSnapshot.getChildren()) {
                        key[i] = d.getKey();
                        i++;
                    }

                }

                @Override
                public void onCancelled(DatabaseError databaseError) {

                }
            });


            for (int counter = key.length - 1; counter >= 0; counter--) {
                viewHolder.setUpDate(first + "/" + key[counter]);
                mDatabase.child(key[counter]).addValueEventListener(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        StringBuilder builder = new StringBuilder();
                        for (DataSnapshot child : dataSnapshot.getChildren()) {
                            builder.append(child.getKey().toString() + "\n\n");

                        }
                        viewHolder.setName(builder.toString());
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {

                    }
                });
            }
        }

    };
    upcomingRV.setAdapter(firebaseRecyclerAdapter);
}

Your assistance is appreciated. 感谢您的协助。

You're being bitten by asynchronous programming 101. You need to make sure that the code that requires the key[] contents is inside the onDataChange that is called once the keys are loaded. 您正在被异步编程101所onDataChange 。您需要确保需要key[]内容的代码在onDataChange 内部 ,一旦加载密钥,该onDataChange就会被调用。

In this case that means it should look something like thisL 在这种情况下,这意味着它应该看起来像这样

mDatabase.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        int i = 0;
        for (DataSnapshot d : dataSnapshot.getChildren()) {
            key[i] = d.getKey();
            i++;
        }
        for (int counter = key.length - 1; counter >= 0; counter--) {
            viewHolder.setUpDate(first + "/" + key[counter]);
            mDatabase.child(key[counter]).addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    StringBuilder builder = new StringBuilder();
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        builder.append(child.getKey().toString() + "\n\n");

                    }
                    viewHolder.setName(builder.toString());
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    throw databaseError.toException(); // don't ignore errors        
                }
            });
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法在child()Firebase错误中为参数&#39;pathString&#39;传递null - Can't pass null for argument 'pathString' in child() firebase error Firebase Android 错误:无法通过 null 子项中的参数“pathString” - Firebase Android error: Can't pass null for argument 'pathString' in child 得到错误 Can't pass null for argument 'pathString' in child() - Getting the error Can't pass null for argument 'pathString' in child() java.lang.NullPointerException:无法为child()中的参数&#39;pathString&#39;传递null - java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() 无法为 child() 中的参数“pathString”传递 null 这个问题的解决方案是什么? 问题出在 Postion - Can't pass null for argument 'pathString' in child() What is the solution to this problem? The problem is in Postion 我在此代码中的 child() 错误中无法为参数“pathString”传递 null - I am getting can't pass null for argument 'pathString' in child() error in this code java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() 这个错误显示 - java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() this error is showing java.lang.NullPointerException:不能通过 null 用于子()中的参数“pathString”..? - java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()..? java.lang.NullPointerException:无法为child()中的参数&#39;pathString&#39;传递null-MessagesDbRef中发生错误 - java.lang.NullPointerException: Can't pass null for argument 'pathString' in child() - Error Occurs in MessagesDbRef 我想创建一个类似 Instagram 的评论页面,但不能通过 null 用于 child() 中的参数 'pathString' - I want to make a comment page like Instagram but Can't pass null for argument 'pathString' in child()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM