android - 在 firebase 数据库中搜索返回空值
问题描述
我正在开发一个应用程序,我必须从数据库中获取消息的状态,但是每当我尝试获取和显示数据时,应用程序都会崩溃回到主屏幕。
user = FirebaseAuth.getInstance().getCurrentUser();
String uid = user.getUid();
database = FirebaseDatabase.getInstance();
String msg = textView.getText().toString().trim();
DatabaseReference myRef = database.getReference();
myRef.child("Users").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot db : dataSnapshot.getChildren()) {
if(db.getKey().equals(textView.getText().toString().trim())) {
count=1;
String key = db.getKey();
String key2= db.child(key).child("status").getValue(String.class);
Toast.makeText(CheckMsg.this,key2, Toast.LENGTH_SHORT).show();
}
}
}
数据库格式
解决方案
I am assuming the crash is because key2
is null:
In the for loop
:
Change this:
String key2= db.child(key).child("status").getValue(String.class);
to this:
String key2= db.child("status").getValue(String.class);
推荐阅读
- c++ - 一张地图中保存的不同函数指针签名的地图
- angular - 为什么我不能阻止用户编辑 PrimeNG 表单字段值?
- google-apps-script - 清除窗口关闭 AppScript 上的某些单元格
- php - PHP——删除最后一个元素后数组是否为空?
- javascript - 重新加载 javascript 类,( Uncaught SyntaxError: redeclaration of let 'classname' )
- docker - 使用带有更改脚本路径声明的 jenkins JobDSL multibranchPipelineJob 时出错
- rust - 如何发送正确的 HTTP 请求并处理响应?
- laravel - 有没有办法将来自网络路由的 404 错误仅重定向到主页?
- powershell - 如何使用 PowerShell 脚本从 TFS 下载特定的标记代码?
- javascript - 更新状态后组件不返回新元素?