Firebase实时数据库,从数据到类,怎么做?

时间:2016-09-19 09:58:36

标签: 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;
    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]
}

    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 GPS {

    public List<Double> cordinates;
    public String type;

    public GPS(){
        // neccessary for getValue
    }

    public GPS(List<Double> cordinates,String type){
        this.cordinates = cordinates;
        this.type = type;

    }

    // [START post_to_map]
    @Exclude
    public Map<String, Object> toMap() {
        HashMap<String, Object> result = new HashMap<>();
        result.put("cordiantes", cordinates);
        result.put("type", type);

        return result;
    }
    // [END post_to_map]
}

我在数据库上有这些数据:

DataSnapshot { key = 4, value = {state=on, id_prod={1=2, 10=11, 5=6, 0=1, 11=12, 9=10, 2=3, 13=14, 8=9, 6=7, 12=13, 7=8, 4=5, 3=4}, _id=4, type={0=bevande calde, 1=bevande fredde, 2=merendine}, gps={type=Point, coordinates={0=39.973584, 1=18.121793}}} }

如果我使用此代码:

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 "

为什么它没有正确创建对象? 请帮帮我。

0 个答案:

没有答案