试图从Firebase数据库填充旋转器时的Null指针异常

发布于 2025-02-10 17:34:35 字数 1430 浏览 2 评论 0原文

我试图从Firebase数据库中填充Android Studio项目上的旋转器。但是,在尝试运行该应用程序时,我会遇到一个空指针异常。谁能告诉我我想念什么???

代码:

public void populateSpinner(){
        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        db.child("Collection").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final List<String> collections = new ArrayList<String>();

                for (DataSnapshot collectionSnapshot: dataSnapshot.getChildren()) {
                    String collectionName = collectionSnapshot.child("name ").getValue(String.class);
                    collections.add(collectionName);
                }

                Spinner areaSpinner = (Spinner) findViewById(R.id.collectionList);
                ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(AddItem.this, android.R.layout.simple_spinner_item, collections);
                areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                areaSpinner.setAdapter(areasAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

链接到firebase数据库的图像 [1]: https://i.sstatic.net/scfsi.png

I am attempting to populate a spinner on Android Studio items from a Firebase Database. However, when trying to run the application, I am met with a null pointer exception. Can anyone tell me what I'm missing???

Code:

public void populateSpinner(){
        DatabaseReference db = FirebaseDatabase.getInstance().getReference();
        db.child("Collection").addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                final List<String> collections = new ArrayList<String>();

                for (DataSnapshot collectionSnapshot: dataSnapshot.getChildren()) {
                    String collectionName = collectionSnapshot.child("name ").getValue(String.class);
                    collections.add(collectionName);
                }

                Spinner areaSpinner = (Spinner) findViewById(R.id.collectionList);
                ArrayAdapter<String> areasAdapter = new ArrayAdapter<String>(AddItem.this, android.R.layout.simple_spinner_item, collections);
                areasAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                areaSpinner.setAdapter(areasAdapter);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }

Link to image of firebase database
[1]: https://i.sstatic.net/SCfSI.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

随梦而飞# 2025-02-17 17:34:35

您的孩子名称在那里有空间。

更改

String collectionName = collectionSnapshot.child("name ").getValue(String.class);

String collectionName = collectionSnapshot.child("name").getValue(String.class);

确保您的儿童与您的模型class相同。

Your child name has space there.

Change

String collectionName = collectionSnapshot.child("name ").getValue(String.class);

To

String collectionName = collectionSnapshot.child("name").getValue(String.class);

Please make sure your child is same name as your model class.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文