试图从Firebase数据库填充旋转器时的Null指针异常
我试图从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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的孩子
名称
在那里有空间。更改
以
确保您的
儿童
与您的模型class
相同。Your child
name
has space there.Change
To
Please make sure your
child
is same name as your modelclass
.