Android Firebase实时数据库,从数据到类,如何实现?
早上好,我使用Firebase进行我的大学应用程序项目。 我创建了两个类:Android Firebase实时数据库,从数据到类,如何实现?,android,json,firebase,firebase-realtime-database,Android,Json,Firebase,Firebase Realtime Database,早上好,我使用Firebase进行我的大学应用程序项目。 我创建了两个类: import com.google.firebase.database.Exclude; import java.util.HashMap; import java.util.List; import java.util.Map; /** * Created by marcomameli on 19/09/16. */ public class Machine { public int _id;
import com.google.firebase.database.Exclude;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* Created by marcomameli on 19/09/16.
*/
public class Machine {
public int _id;
public GPS gps;
public List<Integer> id_prod;
public String state;
public List<String> type;
public Machine(){
// neccessary for getValue
}
public Machine(int _id,GPS gps,List<Integer>id_prod,String state, List<String> type){
this._id = _id;
this.gps = gps;
this.id_prod = id_prod;
this.state = state;
this.type = type;
}
// [START post_to_map]
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("_id", _id);
result.put("gps", gps);
result.put("id_prod", id_prod);
result.put("state", state);
result.put("type", type);
return result;
}
// [END post_to_map]
}
如果我使用此代码:
ValueEventListener machineListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Machine machine = dataSnapshot.getValue(Machine.class);
// this is my code to retrieve gps information:
LatLng myposition = new LatLng(machine.gps.cordinates.get(0),machine.gps.cordinates.get(1));
myMarkerPosition = new MarkerOptions().position(myposition).title("Dove siamo ora");
mMap.addMarker(myMarkerPosition);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadMachine:onCancelled", databaseError.toException());
}
};
mPostReference.addValueEventListener(machineListener);
我有一个错误:
" java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference "
为什么不能正确创建对象?
请帮助我。change
Machine=dataSnapshot.getValue(Machine.class)代码>到小写Machine-Machine=dataSnapshot.getValue(Machine.class)
这是android studio中的一个转置错误,它较低。错误日志显示您的坐标列表
为null
。尝试调试它,并检查它是否正在填充或notcordinates是唯一的空列表,其他人拥有所有数据。解决方案:为每个人搜索此问题的解决方案:从firebase,双精度值由Float表示。GPS类中的列表变成了列表
ValueEventListener machineListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// Get Post object and use the values to update the UI
Machine machine = dataSnapshot.getValue(Machine.class);
// this is my code to retrieve gps information:
LatLng myposition = new LatLng(machine.gps.cordinates.get(0),machine.gps.cordinates.get(1));
myMarkerPosition = new MarkerOptions().position(myposition).title("Dove siamo ora");
mMap.addMarker(myMarkerPosition);
}
@Override
public void onCancelled(DatabaseError databaseError) {
// Getting Post failed, log a message
Log.w(TAG, "loadMachine:onCancelled", databaseError.toException());
}
};
mPostReference.addValueEventListener(machineListener);
" java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.Object java.util.List.get(int)' on a null object reference "