gpt4 book ai didi

java - Firebase onDataChange - 空对象引用

转载 作者:行者123 更新时间:2023-11-29 18:33:28 25 4
gpt4 key购买 nike

我想问问是否有人可以为我解决这个问题..因为我一直在搜索并尝试自己修复它但是..没有..

这是我的 logcat 错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.app.mk.transportrider, PID: 23152
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.app.mk.transportrider.Model.Rider.getName()' on a null object reference
at com.app.mk.transportrider.Home$5$1.onDataChange(Home.java:364)
at com.google.firebase.database.zzp.onDataChange(Unknown Source:7)
at com.google.android.gms.internal.firebase_database.zzfc.zza(Unknown Source:13)
at com.google.android.gms.internal.firebase_database.zzgx.zzdr(Unknown Source:2)
at com.google.android.gms.internal.firebase_database.zzhd.run(Unknown Source:71)
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)

这是我的“代码”:

DatabaseReference driverLocation = FirebaseDatabase.getInstance().getReference(Common.driver_tbl);
GeoFire gf = new GeoFire(driverLocation);

GeoQuery geoQuery = gf.queryAtLocation(new GeoLocation(mLastLocation.getLatitude(), mLastLocation.getLongitude()), distance);
geoQuery.removeAllListeners();


geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
@Override
public void onKeyEntered(String key, final GeoLocation location) {


FirebaseDatabase.getInstance().getReference(Common.user_driver_tbl)
.child(key)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

// Same model *users* & *driver*
// using the ssame model ---
Rider rider = dataSnapshot.getValue(Rider.class);

mMap.addMarker(new MarkerOptions()
.position(new LatLng(location.latitude, location.longitude))
.flat(true)
.title(rider.getName())
.snippet("Phone: " + rider.getPhone())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});

(Home.java:364)是行代码.title(rider.getName())

哦..如果还有其他我可以为您提供更多的东西,我会尽快发布。

请理解..我有点想自己弄这些东西,但是,做不到..即使这么简单,我还是卡住了:( ..

谢谢。

最佳答案

在这行代码之前添加“if(dataSnapshot.exists())”..开始工作:)

Rider rider = dataSnapshot.getValue(Rider.class);

mMap.addMarker(new MarkerOptions()
.position(new LatLng(location.latitude, location.longitude))
.flat(true)
.title(rider.getName())
.snippet("Phone: " + rider.getPhone())
.icon(BitmapDescriptorFactory.fromResource(R.drawable.car)));

关于java - Firebase onDataChange - 空对象引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55204429/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com