我正在尝试从firebase节点获取一个值,并通过onKeyEntered()方法获取它以进行进一步的操作。
我知道我需要从此答案(https://stackoverflow.com/a/47853774/10076702)中进行自定义的回调。但是,代码仍然无法按我预期的那样工作。
@Override
public void onKeyEntered(final String key, final GeoLocation location) {
Toast.makeText(StudentMapsActivity.this, "onKeyEntered", Toast.LENGTH_SHORT).show();
readTrack(new MyCallBack() {
@Override
public void onCallBack(String track1) {
try {
String track = track1;
Log.e("onKeyEntered",track);
} catch (Exception e) {
Log.e("onCallBack","No track specified");
}
}
}, key);
}
public interface MyCallBack{
void onCallBack(String track);
}
private void readTrack(final MyCallBack myCallBack, String key){
driversAvailableDatabaseRef.child(key).child(getString(R.string.track)).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String track = dataSnapshot.getValue(String.class);
myCallBack.onCallBack(track);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Log.d("readTrack", databaseError.getMessage());
}
});
}
输入密钥时,我总是吐司“ onKeyEntered”。但是,我在logcat中得到了
2019-04-26 14:50:35.677 30310-30310/com.example.android.gpstrackerdemo E/onKeyEntered: green
2019-04-26 14:51:07.039 30310-30310/com.example.android.gpstrackerdemo E/onCallBack: No track specified
2019-04-26 14:52:06.345 30310-30310/com.example.android.gpstrackerdemo E/onCallBack: No track specified
当密钥已经存在于数据库中时,我得到了轨道名称,但是当输入新密钥时,我得到了“未指定轨道”
这是我的数据库(https://ibb.co/Y77HWqt)
请多多帮助。
编辑: 这是记录错误后得到的结果
readTrack(new MyCallBack() {
@Override
public void onCallBack(String track) {
try {
String track1 = track;
Log.e("onKeyEntered",track1);
} catch (Exception e) {
Log.e("readTrack","error reading track",e);
}
}
}, key);
2019-04-27 11:07:36.646 11454-11454/com.example.android.gpstrackerdemo E/readTrack: error reading track
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.e(Log.java:312)
at com.example.android.gpstrackerdemo.StudentMapsActivity$6.onCallBack(StudentMapsActivity.java:317)
at com.example.android.gpstrackerdemo.StudentMapsActivity$7.onDataChange(StudentMapsActivity.java:366)
at com.google.firebase.database.Query$1.onDataChange(com.google.firebase:firebase-database@@16.1.0:183)
at com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.1.0:75)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.1.0:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.1.0:55)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)