我需要从数据库Firebase检索数据。但是我有问题,因为当我尝试检索数据时,应用程序将停止。
这是restoredPoint.java的代码
final FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference ref = database.getReference("reloadPoint");
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
ReloadPoint rp = dataSnapshot.getValue(ReloadPoint.class);
System.out.println(rp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});
ref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
ReloadPoint newPost = dataSnapshot.getValue(ReloadPoint.class);
System.out.println("Email: " + newPost.getmEmail());
System.out.println("Point: " + newPost.getmPoint());
System.out.println("User id: " + newPost.getmUserid());
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
这是ReloadPoint类的代码。
public class ReloadPoint {
private String mEmail;
private String mPoint;
private String mUserid;
public ReloadPoint(String email, String point, String userid) {
this.mEmail = email;
this.mPoint = point;
this.mUserid = userid;
}
public String getmEmail() {
return mEmail;
}
public void setmEmail(String mEmail) {
this.mEmail = mEmail;
}
public String getmPoint() {
return mPoint;
}
public void setmPoint(String mPoint) {
this.mPoint = mPoint;
}
public String getmUserid() {
return mUserid;
}
public void setmUserid(String mUserid) {
this.mUserid = mUserid;
}
}
这是我要从Firebase检索的东西。 this is the child that I want to retrieve
这是Android系统,如果检索的过程成功,将显示检索到的数据。retrievedPoint.xml
日志猫的结果。Log cat result
请帮助我。
答案 0 :(得分:2)
如错误消息所暗示,您的ReloadPoint
类需要一个无参数的构造函数,例如:
public ReloadPoint() {
}
答案 1 :(得分:2)
请像这样在ReloadPoint类中包含无参数构造函数
public ReloadPoint() {
}
答案 2 :(得分:1)
public class ReloadPoint {
private String mEmail;
private String mPoint;
private String mUserid;
//Ni hang lupa ni. apa la sakinah
//Klau da soklan lagi, hbungi kt insta id: nyrtron
public ReloadPoint(){
}
public ReloadPoint(String email, String point, String userid) {
this.mEmail = email;
this.mPoint = point;
this.mUserid = userid;
}
public String getmEmail() {
return mEmail;
}
public void setmEmail(String mEmail) {
this.mEmail = mEmail;
}
public String getmPoint() {
return mPoint;
}
public void setmPoint(String mPoint) {
this.mPoint = mPoint;
}
public String getmUserid() {
return mUserid;
}
public void setmUserid(String mUserid) {
this.mUserid = mUserid;
}
}
我想我已经找到你的问题了。
DatabaseReference ref = database.getReference().child("reloadPoint").child("Waniyanazahri");
更新的答案: 看起来您还没有叫课。
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//Add this one
ReloadPoint rp = new ReloadPoint();
rp = dataSnapshot.getValue(ReloadPoint.class);
System.out.println(rp);
}
@Override
public void onCancelled(DatabaseError databaseError) {
System.out.println("The read failed: " + databaseError.getCode());
}
});