本文介绍了使用Android从Firebase中检索数据无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  Constants.USER_REF.orderByChild(email ).equalTo(email).addListenerForSingleValueEvent(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.exists()&& dataSnapshot! null){
//此时存在用户,将其存储为currentUser变量
用户currentUser = dataSnapshot.getValue(User.class);

Log.i( THE_SNAPSHOT_AS_STRING :::,dataSnapshot.toString());

//获取下面行的错误!
Log.i(THE_USERS_EMAIL :::,currentUser.getEmail( ));

} else {
//此时用户不存在
Toast.makeText(TourContactActivity.this,No user exists。,Toast.LENGTH_SHORT ).show();

}
}
@Override
public void onCancelled(FirebaseError firebaseError){

$ b});

我也在控制台日志中获得以下内容,所以我知道它正在拉入正确的用户。

  07-13 15:16:18.391 32277-32277 / thepassapp.thompson.com.tomstours I / THE_SNAPSHOT_AS_STRING :::: DataSnapshot {key = users,value = {00845752-985f-4779-8eff-0c1e6a016ad8 = {tour_director_key = 70af128e-777b-4c12-86f6-2952dbdc9185,last_name = River,[email protected],location_latitude = 42.4305,tour_id = n1337e,photo = http://chopu.herokuapp.com/parse/files/2enxs2j21mz9/7304a8917568d366a8ec43f5ab88ac6b_user.jpg,tour_director_name = Chuck Thoms,middle_name =,location_longitude = -73.5103,passenger_id = mcu79,location_updated = 1468360656387,tour_director = f9e3z,first_name = Gene,provider = password}}} 

这是我的User.class

  @JsonIgnoreProperties(ignoreUnknown = true)

public class User {

private String uid;
私人字符串电子邮件;
private String first_name;
private String last_name;

public User(){

//空的默认构造函数,Firebase必须能够反序列化用户

}
public User(String uid,String first_name,String last_name,String email){
this.uid = uid;
this.first_name = first_name;
this.last_name = last_name;
this.email = email;
}
public String getEmail(){
return email;

public String getFirstName(){
return first_name;
}
public String getLastName(){
return last_name;

$ b @Override
public String toString(){
returnUser {uid ='+ uid +',email ='+ email +' ,first_name ='+ first_name +',last_name ='+ last_name +\'};



$ b $ / code $ / pre
$ b $ p我的问题是实际上从User类中检索这些数据。我跟着文档,但不断收到错误:

 致命例外:main 

过程:passengerapp .veriguide.com.veriguidetours,PID:32277
java.lang.NullPointerException:println需要一个消息



任何想法我做错了?注意:我评论了我获得错误的行。

解决方案

不能直接调用 getValue (),因为dataSnapshot有childs,所以你需要在得到值之前迭代子。

  @Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){$ b $ if(dataSnapshot.exists()){
for(DataSnapshot data:dataSnapshot.getChildren()){
User currentUser = data.getValue(User.class);
Log.i(THE_SNAPSHOT_AS_STRIN,data.toString());
Log.i(THE_USERS_EMAIL :::,currentUser.getEmail());
}
} else {
//此时用户不存在。






$ b

此外,你必须为你的 User class,所以这个值可以设置为类。

  public void setEmail(String email){
this.email = email;
}
public void setFirst_name(String first_name){
this.first_name = first_name;
}
public void setLast_name(String last_name){
this.last_name = last_name;

...

希望这有助于:)


I'm attempting to obtain the current user's details by doing so:

Constants.USER_REF.orderByChild("email").equalTo(email).addListenerForSingleValueEvent(new ValueEventListener() {
     @Override
     public void onDataChange(DataSnapshot dataSnapshot) {
         if (dataSnapshot.exists() && dataSnapshot != null) {
             // User exists at this point, store it as currentUser variable.
             User currentUser = dataSnapshot.getValue(User.class);

             Log.i("THE_SNAPSHOT_AS_STRING:::", dataSnapshot.toString());

             // GETTING THE ERROR ON THE LINE BELOW!
             Log.i("THE_USERS_EMAIL:::", currentUser.getEmail());

         } else {
             // User does not exist at this point.
             Toast.makeText(TourContactActivity.this, "No user exists.", Toast.LENGTH_SHORT).show();

         }
      }
      @Override
      public void onCancelled(FirebaseError firebaseError) {

      }
});

I also get the following printed in the console log, so I know it's pulling in the proper user.

07-13 15:16:18.391 32277-32277/thepassapp.thompson.com.tomstours I/THE_SNAPSHOT_AS_STRING:::: DataSnapshot { key = users, value = {00845752-985f-4779-8eff-0c1e6a016ad8={tour_director_key=70af128e-777b-4c12-86f6-2952dbdc9185, last_name=River, [email protected], location_latitude=42.4305, tour_id=n1337e, photo=http://chopu.herokuapp.com/parse/files/2enxs2j21mz9/7304a8917568d366a8ec43f5ab88ac6b_user.jpg, tour_director_name=Chuck Thoms, middle_name=, location_longitude=-73.5103, passenger_id=mcu79, location_updated=1468360656387, tour_director=f9e3z, first_name=Gene, provider=password}} }

Here is my User.class

@JsonIgnoreProperties(ignoreUnknown=true)

public class User {

    private String uid;
    private String email;
    private String first_name;
    private String last_name;

    public User() {

        // empty default constructor, necessary for Firebase to be able to deserialize users

    }
    public User(String uid, String first_name, String last_name, String email) {
        this.uid = uid;
        this.first_name = first_name;
        this.last_name = last_name;
        this.email = email;
    }
    public String getEmail() {
        return email;
    }
    public String getFirstName() {
        return first_name;
    }
    public String getLastName() {
        return last_name;
    }

    @Override
    public String toString() {
        return "User{uid='"+uid+"', email='"+email+"', first_name='"+first_name+"', last_name='"+last_name+"\'}";

    }

}

My problem is actually retrieving this data from the User class. I followed the docs, but keep getting the error:

FATAL EXCEPTION: main

Process: passengerapp.veriguide.com.veriguidetours, PID: 32277
java.lang.NullPointerException: println needs a message

Any ideas on what I'm doing wrong? Note: I commented the line I'm obtaining the error on.

解决方案

You cannot directly call getValue() because the dataSnapshot has childs, so you need to iterate the child before getting the value.

@Override
public void onDataChange(DataSnapshot dataSnapshot) {
    if (dataSnapshot.exists()) {
        for (DataSnapshot data : dataSnapshot.getChildren()) {
            User currentUser = data.getValue(User.class);
            Log.i("THE_SNAPSHOT_AS_STRIN", data.toString());
            Log.i("THE_USERS_EMAIL:::", currentUser.getEmail());
        }
    } else {
        // User does not exist at this point.
    }
}

And also, you have to create setter for your User class so the value can be set to the class.

public void setEmail(String email) {
    this.email = email;
}
public void setFirst_name(String first_name) {
    this.first_name = first_name;
}
public void setLast_name(String last_name) {
    this.last_name = last_name;
}
...

Hope this helps :)

这篇关于使用Android从Firebase中检索数据无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:07