我使用了一个接口从数据库中检索数据并使用它,起初当我使用String时它正在工作但是在我想要占用整个孩子之后我一直得到一个空指针,这里是全班但是空指针位于checkUser
变量
uti
方法中的部分
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;
}
}
答案 0 :(得分:1)
要通过电子邮件地址获取用户,您需要运行Firebase数据库查询。
df1[grepl("^.[^RK]*.$", df1$Peptide), , drop = FALSE]
# Peptide
#1 ABCOIIJUHFSAUJHR
这会给你的听众带来正确的结果。
但是你的FirebaseDatabase
.getInstance()
.getReference("User")
.orderByChild("email")
.equalTo(email)
.addListenerForSingleValueEvent(mValueEventListener);
也无法正常工作。您试图从快照中获取onDataChange
,但它实际上是List
,其中包含字符串键(Map
等),并且属性为值。所以你需要在回调中转换它:
EuEr...