从 Firebase 检索子数据
Retrieve child data from Firebase
我想从我的 firebase 检索数据,但我在子项目(纬度和经度)中得到 null。
我不知道如何访问这些子项。我猜它的问题是在我的两个 类(MyLatLng.class 和 Waypoints.class)
中将 jackson 反序列化为 java
-
JSON 结构
{
"-KGRY2El-uvfSH3LKOLp" : {
"Waypoints" : {
"-KGRY2El-uvfSH3LKOLq" : {
"latitude" : 223,
"longitude" : 31
},
"-KGRY2El-uvfSH3LKOLr" : {
"latitude" : 123,
"longitude" : 23
}
},
"timeStamp" : "2016/04/27 13:00:56"
},
"-KGRY2OhWFJ_1Zd9RDaU" : {
"Waypoints" : {
"-KGRY2OhWFJ_1Zd9RDaV" : {
"latitude" : 223,
"longitude" : 31
},
"-KGRY2OquKzf1EhmLpTl" : {
"latitude" : 123,
"longitude" : 23
}
},
"timeStamp" : "2016/04/27 13:00:57"
}
}
Logcat 错误
java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.rasmusjosefsson.rjcar.Data.Waypoints.getLongitude()' on a null object reference
at com.example.rasmusjosefsson.rjcar.MapListActivity.populateViewHolder(MapListActivity.java:49)
at com.example.rasmusjosefsson.rjcar.MapListActivity.populateViewHolder(MapListActivity.java:41)
at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:191)
父文件
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyLatLng {
@JsonProperty("timeStamp")
private String timeStamp;
@JsonProperty("waypoints")
private Map<String, Waypoints> waypoints;
public MyLatLng(String timeStamp, HashMap<String, Waypoints> waypoints) {
this.timeStamp = timeStamp;
this.waypoints = waypoints;
}
public MyLatLng() {}
public String getTimeStamp() {
return timeStamp;
}
public Map<String, Waypoints> getWaypoints() {
return waypoints;
}
public void setWaypoints(Map<String, Waypoints> waypoints) {
this.waypoints = waypoints;
}
}
儿童数据
@JsonIgnoreProperties(ignoreUnknown = true)
public class Waypoints {
@JsonProperty("longitude")
private double longitude;
@JsonProperty("latitude")
private double latitude;
public Waypoints(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public Waypoints() {
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
Activity
public class MapListActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FirebaseRecyclerAdapter<MyLatLng, LatLngViewHolder> mRecycleViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://shining-inferno-2821.firebaseio.com/");
Query locRef = ref.limitToLast(10);
mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(false);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setReverseLayout(false);
mRecyclerView.setLayoutManager(manager);
mRecycleViewAdapter = new FirebaseRecyclerAdapter<MyLatLng, LatLngViewHolder>(MyLatLng.class, R.layout.list_item, LatLngViewHolder.class, locRef) {
@Override
protected void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLng myLatLng, int i) {
String key = this.getRef(i).getKey();
latLngViewHolder.locationA.setText(key);
latLngViewHolder.locationB.setText(String.valueOf(myLatLng.getWaypoints().get(key).getLongitude()));
latLngViewHolder.date.setText(String.valueOf(myLatLng.getTimeStamp()));
}
};
mRecyclerView.setAdapter(mRecycleViewAdapter);
}
}
更新:
错误钥匙的图片。
Click here
您似乎正在使用 MyLatLng
的密钥尝试访问 Waypoints
。让我们稍微重写一下您的代码,使其更加明显:
protected void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLng myLatLng, int i) {
String myLatLongKey = this.getRef(i).getKey();
latLngViewHolder.locationA.setText(myLatLongKey);
WayPoints myWayPoint = myLatLng.getWaypoints().get(myLatLongKey);
latLngViewHolder.locationB.setText(String.valueOf(myWayPoint.getLongitude()));
latLngViewHolder.date.setText(String.valueOf(myLatLng.getTimeStamp()));
}
因为那个键没有路径点(因为它是 MyLatLng
的键)Map.get()
returns null 如果你尝试调用方法,你会得到一个空指针异常
我不知道您的代码要用 waypoints 做什么,但是您会希望使用 Map
class 中的操作来编写该代码。例如
String waypointsMsg = "";
for (WayPoints wayPoint: myLatLng.getWaypoints()) {
waypointsMsg += "("+wayPoint.getLongitude()+","+wayPoint.getLatitude()+")\n";
}
latLngViewHolder.locationB.setText(waypointsMsg);
我想从我的 firebase 检索数据,但我在子项目(纬度和经度)中得到 null。
我不知道如何访问这些子项。我猜它的问题是在我的两个 类(MyLatLng.class 和 Waypoints.class)
中将 jackson 反序列化为 java-
JSON 结构
{
"-KGRY2El-uvfSH3LKOLp" : {
"Waypoints" : {
"-KGRY2El-uvfSH3LKOLq" : {
"latitude" : 223,
"longitude" : 31
},
"-KGRY2El-uvfSH3LKOLr" : {
"latitude" : 123,
"longitude" : 23
}
},
"timeStamp" : "2016/04/27 13:00:56"
},
"-KGRY2OhWFJ_1Zd9RDaU" : {
"Waypoints" : {
"-KGRY2OhWFJ_1Zd9RDaV" : {
"latitude" : 223,
"longitude" : 31
},
"-KGRY2OquKzf1EhmLpTl" : {
"latitude" : 123,
"longitude" : 23
}
},
"timeStamp" : "2016/04/27 13:00:57"
}
}
Logcat 错误
java.lang.NullPointerException: Attempt to invoke virtual method 'double com.example.rasmusjosefsson.rjcar.Data.Waypoints.getLongitude()' on a null object reference
at com.example.rasmusjosefsson.rjcar.MapListActivity.populateViewHolder(MapListActivity.java:49)
at com.example.rasmusjosefsson.rjcar.MapListActivity.populateViewHolder(MapListActivity.java:41)
at com.firebase.ui.FirebaseRecyclerAdapter.onBindViewHolder(FirebaseRecyclerAdapter.java:191)
父文件
@JsonIgnoreProperties(ignoreUnknown = true)
public class MyLatLng {
@JsonProperty("timeStamp")
private String timeStamp;
@JsonProperty("waypoints")
private Map<String, Waypoints> waypoints;
public MyLatLng(String timeStamp, HashMap<String, Waypoints> waypoints) {
this.timeStamp = timeStamp;
this.waypoints = waypoints;
}
public MyLatLng() {}
public String getTimeStamp() {
return timeStamp;
}
public Map<String, Waypoints> getWaypoints() {
return waypoints;
}
public void setWaypoints(Map<String, Waypoints> waypoints) {
this.waypoints = waypoints;
}
}
儿童数据
@JsonIgnoreProperties(ignoreUnknown = true)
public class Waypoints {
@JsonProperty("longitude")
private double longitude;
@JsonProperty("latitude")
private double latitude;
public Waypoints(double latitude, double longitude) {
this.latitude = latitude;
this.longitude = longitude;
}
public Waypoints() {
}
public double getLongitude() {
return longitude;
}
public double getLatitude() {
return latitude;
}
public void setLongitude(double longitude) {
this.longitude = longitude;
}
}
Activity
public class MapListActivity extends AppCompatActivity {
private RecyclerView mRecyclerView;
private FirebaseRecyclerAdapter<MyLatLng, LatLngViewHolder> mRecycleViewAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
Firebase.setAndroidContext(this);
Firebase ref = new Firebase("https://shining-inferno-2821.firebaseio.com/");
Query locRef = ref.limitToLast(10);
mRecyclerView = (RecyclerView) findViewById(R.id.card_recycler_view);
mRecyclerView.setHasFixedSize(false);
LinearLayoutManager manager = new LinearLayoutManager(this);
manager.setReverseLayout(false);
mRecyclerView.setLayoutManager(manager);
mRecycleViewAdapter = new FirebaseRecyclerAdapter<MyLatLng, LatLngViewHolder>(MyLatLng.class, R.layout.list_item, LatLngViewHolder.class, locRef) {
@Override
protected void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLng myLatLng, int i) {
String key = this.getRef(i).getKey();
latLngViewHolder.locationA.setText(key);
latLngViewHolder.locationB.setText(String.valueOf(myLatLng.getWaypoints().get(key).getLongitude()));
latLngViewHolder.date.setText(String.valueOf(myLatLng.getTimeStamp()));
}
};
mRecyclerView.setAdapter(mRecycleViewAdapter);
}
}
更新:
错误钥匙的图片。 Click here
您似乎正在使用 MyLatLng
的密钥尝试访问 Waypoints
。让我们稍微重写一下您的代码,使其更加明显:
protected void populateViewHolder(LatLngViewHolder latLngViewHolder, MyLatLng myLatLng, int i) {
String myLatLongKey = this.getRef(i).getKey();
latLngViewHolder.locationA.setText(myLatLongKey);
WayPoints myWayPoint = myLatLng.getWaypoints().get(myLatLongKey);
latLngViewHolder.locationB.setText(String.valueOf(myWayPoint.getLongitude()));
latLngViewHolder.date.setText(String.valueOf(myLatLng.getTimeStamp()));
}
因为那个键没有路径点(因为它是 MyLatLng
的键)Map.get()
returns null 如果你尝试调用方法,你会得到一个空指针异常
我不知道您的代码要用 waypoints 做什么,但是您会希望使用 Map
class 中的操作来编写该代码。例如
String waypointsMsg = "";
for (WayPoints wayPoint: myLatLng.getWaypoints()) {
waypointsMsg += "("+wayPoint.getLongitude()+","+wayPoint.getLatitude()+")\n";
}
latLngViewHolder.locationB.setText(waypointsMsg);