-1
我試圖從使用電子郵件搜索來查找用戶ID並使用該ID來獲得用戶的子節點火力檢索用戶的數據,所以我使用的代碼來做到這一點:爲什麼當我從Firebase獲取數據時,它返回NULL?
mSignin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEmailStr = SignupActivityFragment.removeSpaces(mEmail.getText().toString());
mPasswordStr = mPassword.getText().toString();
mAuth.signInWithEmailAndPassword(mEmailStr, mPasswordStr).addOnCompleteListener(getActivity(), new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(Task<AuthResult> task) {
if (task.isSuccessful()){
getExistingUserInfo();
if(User!=null) {
sglstr = User.get("sgl");
if (sglstr.equals("true")) {
Intent intent = new Intent(getActivity(), ControllerSGL.class);
intent.putExtra("user", User);
startActivity(intent);
} else if (sglstr.equals("false")) {
Intent intent = new Intent(getActivity(), ControllerStudent.class);
intent.putExtra("user", User);
startActivity(intent);
}
}
else {
Toast.makeText(getActivity(), "NULL!!!!!!", Toast.LENGTH_SHORT).show();
}
Toast.makeText(getActivity(), "Success", Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(getActivity(), "Not Success", Toast.LENGTH_SHORT).show();
}
}
});
}
});
和getExistingUserInfo
方法
public void getExistingUserInfo(){
FirebaseDatabase database = FirebaseDatabase.getInstance();
database.getReference().child("allUsers").orderByChild("emailAddress")
.equalTo(mEmailStr)
.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot childSnapshot : dataSnapshot.getChildren()) {
uid = childSnapshot.getKey();
Log.v("HimaAbosalem",uid);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
database.getReference("allUsers").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get user data from dataSnapshot
User=(HashMap<String,String>)dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我需要使用HashMap
發送數據丟意圖,但我給了我,java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
在getExistingUserInfo
方法的這一部分
database.getReference("allUsers").child(uid).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
//get user data from dataSnapshot
User=(HashMap<String,String>)dataSnapshot.getValue();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
我認爲問題是uid值等於空,但我做了一個日誌,它不是,我試圖發送該uid向另一個活動投擲意圖,我收到它發現它不是空的,但當我試圖獲得在新的活動中的數據,它給出了相同的錯誤!
http://ideone.com/PPHi95 ...明顯'getExistingUserInfo'什麼都不做,只是調用異步方法,不要等到它們完成... – Selvin
看到這個問題的答案類似的問題:http://stackoverflow.com/a/39472821/4815718 –
如何做到這一點「處理這種行爲的方法是將程序的流程從」首先獲取朋友列表中重新組織「,然後一起做xyz「到」每當朋友列表發生變化時,用xyz做它「。」在我的案例中,我已經有了用戶,我想要檢索它的數據! –