[英]Can't receive data from Firebase database
to make it short.使其简短。 The app crashes when I want to receive data from the Firebase database.
当我想从 Firebase 数据库接收数据时,应用程序崩溃。 Here is the code:
这是代码:
database = FirebaseDatabase.getInstance();
commu = database.getReference("Community");
longi = voteLoni.getText().toString();
lati = voteLati.getText().toString();
final String latiLong = lati + " - " + longi;
commu.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Community")
.child(latiLong);
ref.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
count= dataSnapshot.child("count").getValue(long.class);
....
Structure: The childs of Community are coordinations结构:社区的孩子是协调人
DB
- Community
- 89.8 - 90.9
- count: 4
- example: 2
- example2: 4
- example3: 8
- 120.9 - 89.9
- count: 45
- example: 24
- example2: 40
- example3: 81
And that's the error:这就是错误:
Process: package, PID: 21441
java.lang.NullPointerException: Attempt to invoke virtual method 'long java.lang.Long.longValue()' on a null object reference
at app.Activity.Real$2$1.onDataChange(Real.java:122)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.0.3:183)
at com.google.firebase.database.obfuscated.zzap.zza(com.google.firebase:firebase-database@@16.0.3:75)
at com.google.firebase.database.obfuscated.zzca.zza(com.google.firebase:firebase-database@@16.0.3:63)
at com.google.firebase.database.obfuscated.zzcd$1.run(com.google.firebase:firebase-database@@16.0.3:55)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:251)
at android.app.ActivityThread.main(ActivityThread.java:6589)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
But in another activity I'm using the same code count= dataSnapshot.child("count").getValue(long.class);
但在另一个活动中,我使用相同的代码
count= dataSnapshot.child("count").getValue(long.class);
- to count the clicks of the user - and it is working... - 计算用户的点击次数 - 它正在工作......
Thanks in advance提前致谢
Edit: I've added the second child and some values to the database.编辑:我已将第二个孩子和一些值添加到数据库中。 The
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Community").child(latiLong);
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("Community").child(latiLong);
is edited too, but this isn't correct?也被编辑了,但这不正确吗?
There is no need to write nested listeners..无需编写嵌套侦听器..
If you have this structure:如果你有这个结构:
DB
- Community
- 1
- count: 4
And you do not have access the the child 1
, then just do the following:并且您无权访问 child
1
,那么只需执行以下操作:
database = FirebaseDatabase.getInstance();
commu = database.getReference("Community");
commu.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot ds : dataSnapshot.getChildren()){
count= ds.child("count").getValue(Long.class);
}
}
.....
This will retrieve the value of count
这将检索
count
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.