所以我有一个应用程序,为此,我在checkData()
中运行方法onCreate
以确保所需的子节点存在,如果不存在,则创建它们(因为我想确保一切都适用于第一个用户)。
我在updateUI()
中致电onStart
。这将从相应节点检索数据并分别更新几个文本字段。
在添加了一些不使用这些方法之一的代码后,我突然开始收到错误(即使删除了所述代码后错误仍然存在,所以不是问题)。
如果我删除updateUI()
电话,节点会正确创建,并且所有内容都可以在后续的程序运行中运行,但在updateUI()
之后立即调用checkData()
告诉我我的数据不存在,也不创建节点。我找不到这里出了什么问题,我不知所措。
public void checkData()
{
//Checks if the "users" table exists in the database, and populates it with the current user if it doesn't
// mDatabase.addListenerForSingleValueEvent(this);
mDatabase.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if (!dataSnapshot.hasChild("users"))
{
Calendar cal = Calendar.getInstance();
User user = new User(mUser.getUid(), mUser.getDisplayName(), mUser.getEmail(), "", "Chennai", cal);
mDatabase.child("users").child(mUser.getUid()).setValue(user);
mUsers = mDatabase.child("users");
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
//Checks if the current user exists in the "users" table, and adds it if it doesn't
mUsers.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot snapshot)
{
//Log.e("Data Change", snapshot.getValue().toString());
if (!snapshot.hasChild(mUser.getUid()))
{
Calendar cal = Calendar.getInstance();
User user = new User(mUser.getUid(), mUser.getDisplayName(), mUser.getEmail(), "", "Chennai", cal);
mUsers.child(mUser.getUid()).setValue(user);
}
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
}
public void updateUI()
{
Log.e("Update UI", "Called");
eText_passName = findViewById(R.id.eText_passName);
eText_passName.setText(mUser.getDisplayName());
eText_date = findViewById(R.id.eText_Date);
eText_time = findViewById(R.id.eText_Time);
//Gets DateTime String and sets it in the fields
mCurrentUser = mUsers.child(mUser.getUid());
mCurrentUser.addListenerForSingleValueEvent(new ValueEventListener()
{
@Override
public void onDataChange(DataSnapshot dataSnapshot)
{
User u = dataSnapshot.getValue(User.class);
String dt = u.getDt();
String dtArr[] = dt.split(" ");
eText_date.setText(dtArr[0]);
eText_time.setText(dtArr[1]);
}
@Override
public void onCancelled(DatabaseError databaseError)
{
}
});
}
错误:
12-29 00:54:30.720 26411-26411/com.namsnath.cabplanner E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.namsnath.cabplanner, PID: 26411
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.namsnath.cabplanner.User.getDt()' on a null object reference
at com.namsnath.cabplanner.MainActivity$3.onDataChange(MainActivity.java:152)
at com.google.firebase.database.zzp.onDataChange(Unknown Source)
at com.google.android.gms.internal.zzegf.zza(Unknown Source)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source)
at com.google.android.gms.internal.zzeig.run(Unknown Source)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6123)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:757)
错误是在updateUI()中调用u.getDt() 似乎数据写入在某处被中断,但我找不到哪里。日志也没有显示出任何结果。
编辑: 这可能是因为互联网连接缓慢,在调用数据之前不允许数据库更新? 如果是这样,Firebase是否应该存储本地副本?这是不是被访问了?
编辑2:
public class User
{
String uid, username, email, group, destination;
//CustomCal c;
String dt;
User()
{}
User(String uid, String username, String email, String group, String destination, Calendar cal)
{
this.uid = uid;
this.username = username;
this.email = email;
this.group = group;
this.destination = destination;
SimpleDateFormat format = new SimpleDateFormat("dd-MM-yyyy KK:mm");
dt = null;
try
{
dt = format.format(cal.getTime());
}
catch (Exception e)
{
}
/*int year = cal.get(Calendar.YEAR);
int day = cal.get(Calendar.DATE);
int month = cal.get(Calendar.MONTH);*/
}
}
我写的数据是这种形式:
cabplanner
users
userUID
Data from user class
答案 0 :(得分:1)
datasnapshot.getValue可能会返回多个结果。试试这个:
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
User u = snapshot.getValue(User.class);
...
}