gpt4 book ai didi

android- snapshot.getvalue 指向空指针

转载 作者:行者123 更新时间:2023-11-30 00:00:57 24 4
gpt4 key购买 nike

我使用一个接口(interface)从数据库中检索数据并使用它,起初当我使用 String 时它可以工作但是在我想带走整个 child 之后我一直得到一个空指针,这是整个类但是空指针在 uti 变量中的 checkUser 方法中的部分

package com.android.pfe.other;

import android.support.annotation.Keep;
import android.util.Log;
import android.widget.Toast;

import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.IgnoreExtraProperties;
import com.google.firebase.database.ValueEventListener;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Hashtable;
import java.util.List;

/**
* Created by SADA INFO on 13/04/2018.
*/
@IgnoreExtraProperties
@Keep
public class User implements Serializable {
private static final String TAG ="UserClass" ;
public String username;
public String email;
public ArrayList<User> contact;
public String Uid;
public List article;
public DatabaseReference mDatabase;
public ArrayList<User> UserList;
public User uti;
public User() {
// Default constructor required for calls to DataSnapshot.getValue(com.android.pfe.other.User.class)
}

public User(String username, String email) {
this.username = username;
this.email = email;



}

public User(String username, String email,String uid) {
this.username = username;
this.email = email;
this.contact=new ArrayList<User>();
this.Uid=Uid;


}

public String getUsername() {
return username;
}

public void setUsername(String username) {
this.username = username;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

public void addUser(String UserId, String name, String email) {
mDatabase = FirebaseDatabase.getInstance().getReference("User");
User user = new User(name, email,UserId);
mDatabase.child(UserId).setValue(user);

}
public void addFriend(String UserId, final String email)
{
mDatabase = FirebaseDatabase.getInstance().getReference("User");
DatabaseReference user = mDatabase.child(UserId);
final DatabaseReference friendlist = user.child("contact");
checkUser(email, new ICheckUserListener() {
@Override
public void onSuccess(final User value) {
friendlist.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {

ArrayList<User> list =(ArrayList<User>) dataSnapshot.getValue();

//this is where value is null
if(list==null&&value!=null)
{
list=new ArrayList<User>();
User user=new User(value.username,email);
list.add(user);
friendlist.setValue(list);
}

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

@Override
public void onError(Exception e) {

}
});

}
public void checkUser(String email, final ICheckUserListener listener) {
ValueEventListener mValueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
// listener.onSuccess(dataSnapshot.exists() ? dataSnapshot : null);
//uti is always null
uti=dataSnapshot.getValue(User.class);
listener.onSuccess(uti);
if(uti==null) {
Log.w(TAG, "user pas trouvé");
}
// addFriend(uti.getEmail(),uti.getUsername());

}

@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "loadUser:onCancelled", databaseError.toException());
listener.onError(databaseError.toException());
}
};
FirebaseDatabase
.getInstance()
.getReference("User")
.equalTo(email)
.addListenerForSingleValueEvent(mValueEventListener);
}


public void getFriends(String UserId){
mDatabase = FirebaseDatabase.getInstance().getReference("User");
DatabaseReference user = mDatabase.child(UserId);
final DatabaseReference friendlist = user.child("contact");
friendlist.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
UserList=(ArrayList)dataSnapshot.getValue();

}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

public ArrayList<User> getUserList() {
return UserList;
}
}

my database

最佳答案

要通过电子邮件地址获取用户,您需要运行 Firebase 数据库查询。

FirebaseDatabase
.getInstance()
.getReference("User")
.orderByChild("email")
.equalTo(email)
.addListenerForSingleValueEvent(mValueEventListener);

这将为您的听众提供正确的结果。

但是您的 onDataChange 也不会工作。您试图从快照中获取 List,但它实际上是一个带有字符串键 (EuEr...,等)和属性作为一个值。所以你需要在你的回调中转换它:

public void onDataChange(DataSnapshot dataSnapshot) {
UserList = new ArrayList<User>();
for (DataSnapshot userSnapshot: dataSnapshot.getChildren()) {
System.out.println(userSnapshot.getKey());
UserList.add(userSnapshot.getValue(User.class));
}
}

关于android- snapshot.getvalue 指向空指针,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49930984/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com