我试图通过这样做来获取当前用户的详细信息:
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
关于我做错的任何想法?注意:我评论了我在获取错误的行。
答案 0 :(得分:3)
您不能直接调用getValue()
因为dataSnapshot有子节点,所以您需要在获取值之前迭代子节点。
@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;
}
...
希望这会有所帮助:)
答案 1 :(得分:0)
当前用户可以通过
获得 FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if (user != null) {
userEmail = user.getEmail();
userName = user.getDisplayName();
}