每次阅读时,Firebase数据库都会返回空值 [英] Firebase Database returns null every time while reading
本文介绍了每次阅读时,Firebase数据库都会返回空值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个类从我的数据库中读取数据,但总是返回 null
。
这里是java file
public class UserActivity extends AppCompatActivity {
TextView textView;
私有FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
私有FirebaseAuth.AuthStateListener mAuthListener;
私人DatabaseReference myRef;
private String userID;
private String TAG =hifiwi;
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
textView =(TextView)findViewById(R.id.textView);
////////////////////////////////////////// ////////////////////
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener(){
@Override $ b $ public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth){
FirebaseUser user = mAuth.getCurrentUser();
if(user!= null){
Toast.makeText(UserActivity.this,用user.getEmail(),Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UserActivity.this,成功退出...,Toast.LENGTH_SHORT).show();
}
}
};
myRef.addValueEventListener(new ValueEventListener(){
@Override $ b $ public void onDataChange(DataSnapshot dataSnapshot){
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError){
$ b}
});
////////////////////////////////////// /////////////////////////
}
private void showData( (DataSnapshot ds:dataSnapshot.getChildren()){
UserInformation uInfo = new UserInformation();
uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail());
uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName());
uInfo.setPassword(ds.child(userID).getValue(UserInformation.class).getPassword());
uInfo.setPhoneno(ds.child(userID).getValue(UserInformation.class).getPhoneno());
Log.i(TAG,+ uInfo.getEmail()); //总是返回null
Log.i(TAG,+ uInfo.getName()); //总是返回null
Log.i(TAG,+ uInfo.getPassword()); //总是返回null
Log.i(TAG,+ uInfo.getPhoneno()); //总是返回null
$ b $覆盖b $ b保护无效onStart(){
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
@Override
protected void onStop(){
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
$ p $日志语句总是返回 null
$ b 这是我在 t2-login
(项目名称)下的数据库结构。
$ $ $
$用户$ {
8dQ8unnqQXa69nkxUFKrcgiTM3S2:{
emailuser:sam @ gmail.com,
mobileUser:1234567899,
nameuser:sam,
passworduser:sammypp
},
DIMBAk4CHZdWGbxN9kPESwIrw9b2:{
emailuser:hsjsjsjjsjsj@mdm.mdmdmdmndn,
mobileUser:9999999999,
nameuser:hshs,
passworduser:ppppppl
},
YJhf16ZyfWUn1Dou3BPcIkSSmVm1:{
emailuser:test2@gmail.com,
mobileUser:1234567892,
nameuser:test2,
passworduser:test2pp
},
yQEFEe5x06hvSZP18Mmb6OfNqnB2:{
emailuser:test1 @ gmail。 com,
mobileUse r:1234567891,
nameuser:test1,
passworduser:test1pp
}
}
}
UserInformation.class
package com.rishav.t2;
/ **
*由rishav于6/27/2017创建。
* /
public class UserInformation {
private String email;
私人字符串名称;
private String phoneno;
私人字符串密码;
$ b public UserInformation(){
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPhoneno(){
return phoneno;
public void setPhoneno(String phoneno){
this.phoneno = phoneno;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
解决方案
Set myRef: myRef = FirebaseDatabase.getInstance()。getReference(Users)。child(user.getUid());
$ p
$ b pre $ private $ show data(dataSnapshot dataSnapshot){
UserInformation uInfo = ds.getValue(UserInformation.class);
Log.i(TAG,+ uInfo.getEmail()); //总是返回null
Log.i(TAG,+ uInfo.getName()); //总是返回null
Log.i(TAG,+ uInfo.getPassword()); //总是返回null
Log.i(TAG,+ uInfo.getPhoneno()); //总是返回null
拥有UserInformation类变量匹配firebase数据库中存在的键名:
public class UserInformation {
public String emailuser;
public String nameuser;
public String mobileUser;
public String passworduser;
$ b public UserInformation(){
}
public String getEmail(){
return email;
}
public void setEmail(String email){
this.email = email;
}
public String getName(){
return name;
}
public void setName(String name){
this.name = name;
}
public String getPhoneno(){
return phoneno;
public void setPhoneno(String phoneno){
this.phoneno = phoneno;
}
public String getPassword(){
return password;
}
public void setPassword(String password){
this.password = password;
}
I have a class to read data from my database but it always returns null
.
Here's the java file
public class UserActivity extends AppCompatActivity {
TextView textView;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;
private String userID;
private String TAG = "hifiwi";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_user);
textView = (TextView) findViewById(R.id.textView);
//////////////////////////////////////////////////////////////
mAuth = FirebaseAuth.getInstance();
mFirebaseDatabase = FirebaseDatabase.getInstance();
myRef = mFirebaseDatabase.getReference();
FirebaseUser user = mAuth.getCurrentUser();
userID = user.getUid();
mAuthListener = new FirebaseAuth.AuthStateListener() {
@Override
public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
Toast.makeText(UserActivity.this, "Signed in with " + user.getEmail(), Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(UserActivity.this, "Successfully signed out...", Toast.LENGTH_SHORT).show();
}
}
};
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
showData(dataSnapshot);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
///////////////////////////////////////////////////////////////
}
private void showData(DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
UserInformation uInfo = new UserInformation();
uInfo.setEmail(ds.child(userID).getValue(UserInformation.class).getEmail());
uInfo.setName(ds.child(userID).getValue(UserInformation.class).getName());
uInfo.setPassword(ds.child(userID).getValue(UserInformation.class).getPassword());
uInfo.setPhoneno(ds.child(userID).getValue(UserInformation.class).getPhoneno());
Log.i(TAG, " " + uInfo.getEmail()); //Always returning null
Log.i(TAG, " " + uInfo.getName()); //Always returning null
Log.i(TAG, " " + uInfo.getPassword()); //Always returning null
Log.i(TAG, " " + uInfo.getPhoneno()); //Always returning null
}
}
@Override
protected void onStart() {
super.onStart();
mAuth.addAuthStateListener(mAuthListener);
}
@Override
protected void onStop() {
super.onStop();
mAuth.removeAuthStateListener(mAuthListener);
}
}
The log statements always return null.
This is my database structure under t2-login
(the project name).
{
"Users" : {
"8dQ8unnqQXa69nkxUFKrcgiTM3S2" : {
"emailuser" : "sam@gmail.com",
"mobileUser" : "1234567899",
"nameuser" : "sam",
"passworduser" : "sammypp"
},
"DIMBAk4CHZdWGbxN9kPESwIrw9b2" : {
"emailuser" : "hsjsjsjjsjsj@mdm.mdmdmdmndn",
"mobileUser" : "9999999999",
"nameuser" : "hshs",
"passworduser" : "ppppppl"
},
"YJhf16ZyfWUn1Dou3BPcIkSSmVm1" : {
"emailuser" : "test2@gmail.com",
"mobileUser" : "1234567892",
"nameuser" : "test2",
"passworduser" : "test2pp"
},
"yQEFEe5x06hvSZP18Mmb6OfNqnB2" : {
"emailuser" : "test1@gmail.com",
"mobileUser" : "1234567891",
"nameuser" : "test1",
"passworduser" : "test1pp"
}
}
}
The UserInformation.class
package com.rishav.t2;
/**
* Created by rishav on 6/27/2017.
*/
public class UserInformation {
private String email;
private String name;
private String phoneno;
private String password;
public UserInformation() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
解决方案
Set myRef to: myRef = FirebaseDatabase.getInstance().getReference("Users").child(user.getUid());
Also change this:
private void showData(DataSnapshot dataSnapshot) {
UserInformation uInfo = ds.getValue(UserInformation.class);
Log.i(TAG, " " + uInfo.getEmail()); //Always returning null
Log.i(TAG, " " + uInfo.getName()); //Always returning null
Log.i(TAG, " " + uInfo.getPassword()); //Always returning null
Log.i(TAG, " " + uInfo.getPhoneno()); //Always returning null
}
Have UserInformation class variables match the key names that exist in the firebase database:
public class UserInformation {
public String emailuser;
public String nameuser;
public String mobileUser;
public String passworduser;
public UserInformation() {
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhoneno() {
return phoneno;
}
public void setPhoneno(String phoneno) {
this.phoneno = phoneno;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
这篇关于每次阅读时,Firebase数据库都会返回空值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文