gpt4 book ai didi

java - 在 Android 无法正常工作的情况下从 Firebase 检索数据

转载 作者:行者123 更新时间:2023-11-29 04:43:07 25 4
gpt4 key购买 nike

我试图通过这样做来获取当前用户的详细信息:

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

}
});

我还在控制台日志中打印了以下内容,所以我知道它正在吸引正确的用户。

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=rivers@gmail.com, 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 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+"\'}";

}

}

我的问题实际上是从 User 类中检索此数据。我按照文档进行操作,但不断收到错误消息:

FATAL EXCEPTION: main

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

对我做错了什么有什么想法吗?注意:我评论了我收到错误的行。

最佳答案

您不能直接调用getValue(),因为dataSnapshot 有 child ,所以您需要在获取值之前迭代 child 。

@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.
}
}

此外,您还必须为您的 User 类创建 setter,以便可以将值设置到该类。

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;
}
...

希望这有帮助:)

关于java - 在 Android 无法正常工作的情况下从 Firebase 检索数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38360070/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com