我的应用程序允许用户在Google地图上添加标记时添加有关地点的详细信息。这些详细信息存储在Firebase中,所有详细信息均为null。
这是更新的树:
{
"Algiers" : {
"Description" : "Capital of Algeria",
"Latitude" : 36.77202162442747,
"Longitude" : 3.0560945346951485,
"Rating" : "9",
"Type" : "Town"
},
"Brussels" : {
"Description" : "Capital of Belgium",
"Latitude" : 50.850103621427685,
"Longitude" : 4.3520765751600266,
"Rating" : "8",
"Type" : "Town"
},
"Burger King" : {
"Description" : "Fast but not healthy food",
"Latitude" : 51.236331,
"Longitude" : -0.575385,
"Rating" : 5.29,
"Type" : "Restaurant"
},
"Chippy Pizza" : {
"Description" : "Best American pizza ever",
"Latitude" : 36.72722332469427,
"Longitude" : 3.0647138133645058,
"Rating" : "10",
"Type" : "Restaurant"
},
"European Parliament" : {
"Description" : "Some decisions about Europe are done there",
"Latitude" : 50.83868986886192,
"Longitude" : 4.375510700047016,
"Rating" : "8",
"Type" : "Museum"
},
"Friary Center McDonalds" : {
"Description" : "Good food",
"Latitude" : 51.23544690507358,
"Longitude" : -0.5764660611748695,
"Rating" : "7.4",
"Type" : "Restaurant"
},
"Guildford Town Center" : {
"Description" : "Town center of Guildford",
"Latitude" : 51.23622,
"Longitude" : -0.570409,
"Rating" : 8.74,
"Type" : "Town"
},
"Guildfords Castle" : {
"Description" : "Very old castle",
"Latitude" : 51.23428622281793,
"Longitude" : -0.5723629519343376,
"Rating" : "8",
"Type" : "Museum"
},
"Guildfords Hospital" : {
"Description" : "Treats all types of pathologies",
"Latitude" : 51.240506,
"Longitude" : -0.60838,
"Rating" : "9",
"Type" : "Hospital"
},
"Guildfords Museum" : {
"Description" : "Museum with several exhibitions",
"Latitude" : 51.233797,
"Longitude" : -0.573549,
"Rating" : "7.84",
"Type" : "Museum"
},
"Martyrs Monument" : {
"Description" : "Describes the History of Algeria",
"Latitude" : 36.745560268045196,
"Longitude" : 3.0698012933135033,
"Rating" : "10",
"Type" : "Museum"
},
"Military Hospital" : {
"Description" : "Excellent Hospital",
"Latitude" : 36.71973881144668,
"Longitude" : 3.063882999122143,
"Rating" : "10",
"Type" : "Hospital"
},
"Mount Alvernia Hospital" : {
"Description" : "Small and correct hospital",
"Latitude" : 51.23583085374619,
"Longitude" : -0.5647089332342148,
"Rating" : "7",
"Type" : "Hospital"
},
"Mustapha Bacha Hospital" : {
"Description" : "Complete Hospital",
"Latitude" : 36.76243892522928,
"Longitude" : 3.053263798356056,
"Rating" : "7",
"Type" : "Hospital"
},
"Nandos Guildford" : {
"Description" : "Tasty but expensive chicken",
"Latitude" : 51.236343270137674,
"Longitude" : -0.5743494629859924,
"Rating" : "9",
"Type" : "Restaurant"
},
"Pachéco Hospital" : {
"Description" : "Excellent Hospital",
"Latitude" : 50.854205318946484,
"Longitude" : 4.349818155169487,
"Rating" : "10",
"Type" : "Hospital"
},
"Paris" : {
"Description" : "Capital",
"Latitude" : "48.84191426189115",
"Longitude" : "2.3034103587269783",
"Rating" : "8",
"Type" : "Town"
},
"Starbucks Coffee" : {
"Description" : "All types of coffee are found there",
"Latitude" : 51.235304,
"Longitude" : -0.575049,
"Rating" : 7.84,
"Type" : "Restaurant"
},
"Test 2" : {
"Description" : "Test",
"Latitude" : "26.567103702363973",
"Longitude" : "2.967230938374996",
"Rating" : "8",
"Type" : "Town"
},
"Test Overflow" : {
"Description" : "Test",
"Latitude" : "31.29925316955398",
"Longitude" : "2.439718544483185",
"Rating" : "10",
"Type" : "Town"
},
"Test3" : {
"Description" : "Testf",
"Latitude" : "45.24608905762172",
"Longitude" : "3.4947332739830017",
"Rating" : "8",
"Type" : "Town"
}
}
我正在使用Firebase文档从我的数据库中检索数据(使用onDataChange()
)。
但是,我的应用程序崩溃并打印出以下错误:
05-16 14:37:15.772 13332-13332/org.com1032.flagged_v2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: org.com1032.flagged_v2, PID: 13332
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at org.com1032.flagged_v2.MainActivity$6.onDataChange(MainActivity.java:877)
at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:56)
at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5219)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
在一些Logs之后,我意识到该特定键的所有值都为空。
以下是从Firebase检索数据的代码:
firebaseMarkers.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()) {
String descPath = dataSnapshot1.getKey() + "/Description";
String latPath = dataSnapshot1.getKey() + "/Latitude";
String lonPath = dataSnapshot1.getKey() + "/Longitude";
String ratePath = dataSnapshot1.getKey() + "/Rating";
String typePath = dataSnapshot1.getKey() + "/Type";
Log.v("INSERTING?", "MAYBE");
Log.v("the KEY", dataSnapshot1.getKey());
Log.v("TYPE:", dataSnapshot.child(typePath) + "");
Log.v("NAME:", dataSnapshot1.getKey());
Log.v("LATITUDE:", dataSnapshot.child(latPath).getValue().toString());
Log.v("LONGITUDE:", dataSnapshot.child(lonPath).getValue().toString());
Log.v("RATING:", dataSnapshot.child(ratePath).getValue().toString());
Log.v("DESCRIPTION", dataSnapshot.child(descPath).getValue().toString());
markersDatabase.insertData(dataSnapshot.child(typePath).getValue().toString(), dataSnapshot1.getKey(),
Double.parseDouble(dataSnapshot.child(latPath).getValue().toString()), Double.parseDouble(dataSnapshot.child(lonPath).getValue().toString()),
dataSnapshot.child(ratePath).getValue().toString(), dataSnapshot.child(descPath).getValue().toString());
Log.v("INSERTED!", "YES");
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Log.v("Firebase Error", "Wow, you're asking very deep");
}
});
每次添加数据时,所有内容都会存储到手机中的本地数据库中(之后删除所有重复项)。
以下是将数据添加到firebase中的代码:
fire.child(markerName.toString()).setValue(markerName.toString());
fire.child(markerName.toString()).child("Description").setValue(markerDescription.toString());
fire.child(markerName.toString()).child("Latitude").setValue(String.valueOf(point.latitude));
fire.child(markerName.toString()).child("Longitude").setValue(String.valueOf(point.longitude));
fire.child(markerName.toString()).child("Rating").setValue(markerRating.toString());
fire.child(markerName.toString()).child("Type").setValue(markerType.toString());
MarkerOptions marker = new MarkerOptions().position(new LatLng(point.latitude, point.longitude)).title(markerName);
map.addMarker(marker);
markersDatabase.insertData(markerType, markerName, point.latitude, point.longitude, markerRating, markerDescription);
提前谢谢。
答案 0 :(得分:1)
基本上,我自己找到了答案。
问题在于将数据插入Firebase。我正在使用setValue()
方法,这对我正在使用的值来说并不是最好的方法。
最好的方法是创建一个新的HashMap
,您将在其中放置路径和值,如下例所示:
Map<String, String> map2 = new HashMap<String, String>();
map2.put("Description", markerDescription);
map2.put("Latitude", String.valueOf(point.latitude));
map2.put("Longitude", String.valueOf(point.longitude));
map2.put("Rating", markerRating);
map2.put("Type", markerType);
Firebase fire = new Firebase("********").child(markerName);
fire.setValue(map2);
这基本上是插入数据的最佳方式(至少在我的情况下)。
答案 1 :(得分:0)
同样的事情发生在我身上。 firebase中的数据看起来完全相同。在调试时我可以提取数据,但是在运行时它会返回null。出于某种原因,将hashmap保存到firebase可以正常工作。
BTW,如果你使用,你可以节省一些铸件。
Map<String, Object> markerMap = new HashMap<String, Object>();