我正在尝试从Firebase中检索用户关注者,并将其填充到RecyclerView
上,以便任何人都可以看到类似twitter和Instagram,当我运行该应用程序时它会返回错误。
我的代码:
公共类ViewFollowersFragment扩展Fragment { 私有静态最终String TAG =“ ViewFollowersFragment”;
private CircleImageView myprofileimage;
private TextView username;
private ImageView arrow;
private ListView vfollowerslistview;
private DatabaseReference viewFollowersref,viewUserRef;
private FirebaseAuth mAuth;
private FirebaseListAdapter viewfollowersadapter;
private List<User> mUserList;
private User mUser;
String userid;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_followers, container, false);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
vfollowerslistview = (ListView) view.findViewById(R.id.viewfollowerslistview);
arrow = (ImageView) view.findViewById(R.id.fragfollowersbackarrow);
arrow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().popBackStack();
getActivity().finish();
}
});
mUserList = new ArrayList<>();
mAuth = FirebaseAuth.getInstance();
userid = mAuth.getUid();
viewFollowersref = FirebaseDatabase.getInstance().getReference().child("User_Account_Settings");
viewUserRef = FirebaseDatabase.getInstance().getReference();
setFollowers();
return view;
}
private void setFollowers () {
Query query = FirebaseDatabase.getInstance().getReference()
.child("Followers")
.child(mUser.getUser_id());
FirebaseListOptions<UserAccountSettings> options = new FirebaseListOptions.Builder<UserAccountSettings>()
.setLayout(R.layout.layout_user_listitem)
.setLifecycleOwner(getActivity())
.setQuery(query,UserAccountSettings.class)
.build();
viewfollowersadapter = new FirebaseListAdapter(options) {
@Override
protected void populateView(View v, Object model, final int position) {
String newfol = getRef(position).getKey();
final TextView username = v.findViewById(R.id.username);
final CircleImageView profile_image = v.findViewById(R.id.profile_image);
viewFollowersref.child(newfol).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
if (dataSnapshot.hasChild("username")) {
String myusername = dataSnapshot.child("username").getValue().toString();
username.setText(myusername);
}
if (dataSnapshot.hasChild("profileimage")) {
String myimage = dataSnapshot.child("profileimage").getValue().toString();
if (myimage.isEmpty()) {
profile_image.setImageResource(R.drawable.pro_pic);
} else {
Picasso.with(getActivity()).load(myimage).placeholder(R.drawable.pro_pic).into(profile_image);
}
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
username.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent profileIntent = new Intent(getActivity(), ProfileActivity.class);
profileIntent.putExtra("calling_activity", "Search_Activity");
profileIntent.putExtra("intent_user", mUserList.get(position));
startActivity(profileIntent);
}
});
profile_image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent profileIntent = new Intent(getActivity(), ProfileActivity.class);
profileIntent.putExtra("calling_activity", "Search_Activity");
profileIntent.putExtra("intent_user", mUserList.get(position));
startActivity(profileIntent);
}
});
Query userQuery = viewUserRef
.child("Users")
.orderByChild("user_id");
userQuery.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot singleSnapshot : dataSnapshot.getChildren()) {
Log.d(TAG, "onDataChange: found user: " +
singleSnapshot.getValue(User.class).getUsername());
mUserList.add(singleSnapshot.getValue(User.class));
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
};
vfollowerslistview.setAdapter(viewfollowersadapter);
}
}
我似乎无法放入整个entirw代码。
答案 0 :(得分:0)
根据您的评论,您收到以下错误:
原因:java.lang原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'java.lang.String com.example.hope.myapplication.Models.User.getUser_id()'< / p>
因为您正在调用mUser
方法的getUser_id()
对象的值为null
。发生这种情况是因为您根本没有在代码中初始化User
对象。要解决此问题,请更改以下代码行:
Query query = FirebaseDatabase.getInstance().getReference()
.child("Followers")
.child(mUser.getUser_id());
到
Query query = FirebaseDatabase.getInstance().getReference()
.child("Followers")
.child(mAuth.getCurrentUser().getUid());
其中mAuth.getCurrentUser()
返回一个FirebaseUser
对象,因此您可以获得正确的uid
。