如何在Firebase数据库中接收特定值(读取或接收数据时出现问题)

时间:2017-04-30 03:11:29

标签: android firebase nullpointerexception firebase-realtime-database

我需要从我的数据库中接收这两个值 我需要将这些值放在标记中 而且我不知道如何将收到的数据库的纬度和经度放到标记

db

这是我使用的方法:

          public static DatabaseReference getPostRef() {
    return FirebaseDatabase.getInstance()
            .getReference(Constantes.POSTS_KEY);
}
public static Query getPostQuery() {
    return getPostRef().orderByChild(Constantes.TIME_CREATED_KEY);
}
public static DatabaseReference getMyPostRef() {
    return FirebaseDatabase.getInstance().getReference(Constantes.MY_POSTS)
            .child(getCurrentUser().getEmail().replace(".", ","));
}

// USER ID
public static String getUid() {
    String path = FirebaseDatabase.getInstance().getReference().push().toString();
    return path.substring(path.lastIndexOf("/") + 1);
}

我使用它来发送数据

 FirebaseUtils.getPostRef().child(postUid).setValue(mPost);

这是我的模特课:

 public class Post implements Serializable {

private String photoPortadaUrl;

private String organizadoPor;

private String mes;
private String dia;

private String hora;
private String minutos;

private String titulo;
private String descripicion;
private String costo;

private double latitud;
private double longitud;

public Post(){

}

public Post(String photoPortadaUrl, String organizadoPor, String mes, String dia, String hora, String minutos, String titulo, String descripicion, String costo, double latitud, double longitud) {
    this.photoPortadaUrl = photoPortadaUrl;
    this.organizadoPor = organizadoPor;
    this.mes = mes;
    this.dia = dia;
    this.hora = hora;
    this.minutos = minutos;
    this.titulo = titulo;
    this.descripicion = descripicion;
    this.costo = costo;
    this.latitud = latitud;
    this.longitud = longitud;
}


public String getPhotoPortadaUrl() {
    return photoPortadaUrl;
}

public void setPhotoPortadaUrl(String photoPortadaUrl) {
    this.photoPortadaUrl = photoPortadaUrl;
}

public String getTitulo() {
    return titulo;
}

public void setTitulo(String titulo) {
    this.titulo = titulo;
}

public String getOrganizadoPor() {
    return organizadoPor;
}

public void setOrganizadoPor(String organizadoPor) {
    this.organizadoPor = organizadoPor;
}

public String getMes() {
    return mes;
}

public void setMes(String mes) {
    this.mes = mes;
}

public String getDia() {
    return dia;
}

public void setDia(String dia) {
    this.dia = dia;
}

public String getHora() {
    return hora;
}

public void setHora(String hora) {
    this.hora = hora;
}

public String getMinutos() {
    return minutos;
}

public void setMinutos(String minutos) {
    this.minutos = minutos;
}

public String getDescripicion() {
    return descripicion;
}

public void setDescripicion(String descripicion) {
    this.descripicion = descripicion;
}

public String getCosto() {
    return costo;
}

public void setCosto(String costo) {
    this.costo = costo;
}

public double getLatitud() {
    return latitud;
}

public void setLatitud(double latitud) {
    this.latitud = latitud;
}

public double getLongitud() {
    return longitud;
}

public void setLongitud(double longitud) {
    this.longitud = longitud;
}

}

但是为了接收数据或阅读它们,我这样做:我把相关的代码:

            public class NavActivity extends AppCompatActivity {

private Post mPost;

  protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_nav);
  mPost = new Post();
 }


 public void onMapReady(GoogleMap googleMap) {
   FirebaseUtils.getPostRef().child(postuid).addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        mPost = dataSnapshot.getValue(Post.class);

        // HERE IS THE ERROR
        //When I try to read the data it gives me an exception which I will put down


        /* 
         I try with this 
          but i get the same exception

           Post LPOst = dataSnapshot.getValue(Post.class);

           Log.d("Dep", LPOst.getTitulo());
         */

        Log.d("Dep", mPost.getTitulo());
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});



 }

}

我得到了这个例外:

      java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.octacorp.octa.strank.models.Post.getTitulo()' on a null object reference
                                                                      at com.octacorp.octa.strank.Activities.NavActivity$5.onDataChange(NavActivity.java:333)

我该怎么办?

1 个答案:

答案 0 :(得分:1)

您需要一个值事件侦听器。如果您不希望每次更改时都提取数据,则可以使用单个事件监听器。