我有一个Android应用,目前有约2k活跃用户。它使用Firebase Firestore数据库保存用户数据和其他内容。但是最近我从readUserDataFromDatabase函数中收到了此错误并崩溃报告:
private void readUserDataFromDatabase(String uid) {
db.collection(Constants.PARAM_USERS)
.document(uid) // (MainActivity.java:215)This is where the crash happens
.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserData userData = documentSnapshot.toObject(UserData.class);
if (userData == null)
return;
tvName.setText(getString(R.string.welcome, userData.getName()));
Picasso
.get()
.load(userData.getPhotoUrl())
.placeholder(getResources().getDrawable(R.drawable.place_holder))
.transform(new RoundedCornersTransform())
.into(ivPhoto);
UserData.getInstance().setCredit(userData.getCredit());
UserData.getInstance().setPhotoUrl(userData.getPhotoUrl());
UserData.getInstance().setName(userData.getName());
if (userData.getInstanceId() != null) {
UserData.getInstance().setInstanceId(userData.getInstanceId());
}
}
});
}
此功能在成功完成登录过程后从登录活动重定向后运行:
private void setUser(final FirebaseUser user) {
if (user == null)
return;
UserData.getInstance().setUserId(user.getUid());// I'm using this Uid for create node for user.
DocumentReference docRef = db.collection(Constants.PARAM_USERS).document(user.getUid());
docRef
.get()
.addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
@Override
public void onSuccess(DocumentSnapshot documentSnapshot) {
UserData userData = documentSnapshot.toObject(UserData.class);
if (userData == null) {
createUser(user);
} else {
redirectToHome(); //login succeed
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
}
});
}
并从重定向的活动中调用readUserDataFromDatabase函数,如下所示:
readUserDataFromDatabase(UserData.getInstance().getUserId());
崩溃率大约为%1,但仍然看不到这里缺少的内容。 firebase用户ID是否有可能返回null或登录成功后,readUserDataFromDatabase在
之前运行 UserData.getInstance().setUserId(user.getUid());
功能?我不知道。谢谢。
编辑1:添加了堆栈跟踪。
致命异常:java.lang.RuntimeException:无法启动活动 ComponentInfo {com.alialacan.kpss / com.alialacan.kpss.MainActivity}: java.lang.NullPointerException:提供的文档路径不得为 空值。 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2781) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859) 在android.app.ActivityThread.-wrap11(未知来源) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1592) 在android.os.Handler.dispatchMessage(Handler.java:106) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6518) 在java.lang.reflect.Method.invoke(Method.java) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by java.lang.NullPointerException: Provided document path must not be null. at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:228) at com.google.firebase.firestore.CollectionReference.document(SourceFile:90) at com.alialacan.kpss.MainActivity.readUserDataFromDatabase(MainActivity.java:215) at com.alialacan.kpss.MainActivity.onCreate(MainActivity.java:114) at android.app.Activity.performCreate(Activity.java:7013) at android.app.Activity.performCreate(Activity.java:7004) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1215) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2734) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2859) at android.app.ActivityThread.-wrap11(Unknown Source) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1592) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6518) at java.lang.reflect.Method.invoke(Method.java) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
编辑2:这里是 setUser 函数的调用位置:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
callbackManager.onActivityResult(requestCode, resultCode, data);
if (requestCode == Constants.RC_SIGN_IN) {
GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data);
if (result.isSuccess()) {
GoogleSignInAccount account = result.getSignInAccount();
firebaseAuthWithGoogle(account);
} else {
Toast.makeText(LoginActivity.this, R.string.error_google_login,
Toast.LENGTH_SHORT).show();
hideProgress();
}
}
}
private void handleFacebookAccessToken(AccessToken token) {
Log.d("", "handleFacebookAccessToken:" + token);
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
Log.d("", "signInWithCredential:success");
user = auth.getCurrentUser();
setUser(user);
} else {
setUser(null);
Log.w("", "signInWithCredential:failure", task.getException());
Toast.makeText(LoginActivity.this, R.string.error_email_already_exist,
Toast.LENGTH_SHORT).show();
hideProgress();
}
}
});
}
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
auth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
FirebaseUser user = auth.getCurrentUser();
setUser(user);
} else {
Toast.makeText(LoginActivity.this, R.string.error_google_login,
Toast.LENGTH_SHORT).show();
setUser(null);
hideProgress();
}
}
});
}
答案 0 :(得分:1)
错误消息说此代码正在将null值传递给readUserDataFromDatabase
readUserDataFromDatabase(UserData.getInstance().getUserId());
这意味着UserData.getInstance().getUserId()
返回null。
这里实际上没有足够的代码来完全诊断问题,因为您也没有显示调用setUser
的内容,但是您需要将此代码追溯到源代码。
答案 1 :(得分:1)
对我来说,清除缓存对我有所帮助
collection("Posts").orderBy("timestamp",Query.Direction.DESCENDING)
至collection("Posts").orderBy("like_count",Query.Direction.DESCENDING)