我的朋友代码片段试图在其中使用回收站视图显示朋友列表。
package com.rishav.baatcheet;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.auth.FirebaseAuth;
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.ValueEventListener;
import com.squareup.picasso.Picasso;
import de.hdodenhof.circleimageview.CircleImageView;
public class FreindsFragment extends Fragment {
private RecyclerView mFreindsList;
private DatabaseReference mUsersDatabase;
private DatabaseReference mFreindsDatabase;
private FirebaseAuth mAuth;
private String mCurrent_user_id;
private View mMainView;
private String list_user_id;
public FreindsFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_freinds , container , false);
mFreindsList = (RecyclerView) mMainView.findViewById(R.id.freinds_list);
mAuth = FirebaseAuth.getInstance();
mCurrent_user_id = mAuth.getCurrentUser().getUid();
mFreindsDatabase = FirebaseDatabase.getInstance().getReference().child("Friends").child(mCurrent_user_id);
mFreindsDatabase.keepSynced(true);
mUsersDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
mUsersDatabase.keepSynced(true);
mFreindsList.setHasFixedSize(true);
mFreindsList.setLayoutManager(new LinearLayoutManager(getContext()));
return mMainView;
}
@Override
public void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Friends , FriendsViewHolder> friendsRecyclerAdapter = new FirebaseRecyclerAdapter<Friends, FriendsViewHolder>(
Friends.class,
R.layout.users_single_layout,
FriendsViewHolder.class,
mFreindsDatabase
) {
@Override
protected void populateViewHolder(final FriendsViewHolder friendsViewHolder, final Friends friends, int i) {
friendsViewHolder.setDate(friends.getDate());
list_user_id = getRef(i).getKey();
mUsersDatabase.child(list_user_id).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
final String userName = dataSnapshot.child("name").getValue().toString();
String userThumb = dataSnapshot.child("thumb_image").getValue().toString();
if (dataSnapshot.hasChild("online")) {
Boolean userOnline = (boolean) dataSnapshot.child("online").getValue();
friendsViewHolder.setUserOnline(userOnline);
}
friendsViewHolder.setName(userName);
friendsViewHolder.setUserImage(userThumb);
friendsViewHolder.mView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CharSequence []options = new CharSequence[]{"Open Profile" , "Open Chat"};
final AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Select Options");
builder.setItems(options, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0){
Intent profileIntent = new Intent(getContext() , ProfileActivity.class);
profileIntent.putExtra("user_id" , list_user_id);
startActivity(profileIntent);
}
if (which == 1){
Intent chatIntent = new Intent(getContext() , ChatActivity.class);
chatIntent.putExtra("user_id" , list_user_id);
chatIntent.putExtra("user_name" , userName);
startActivity(chatIntent);
}
}
});
builder.show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
};
mFreindsList.setAdapter(friendsRecyclerAdapter);
}
public static class FriendsViewHolder extends RecyclerView.ViewHolder {
View mView;
public FriendsViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setDate (String date){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_status);
userNameView.setText(date);
}
public void setName(String name){
TextView userNameView = (TextView) mView.findViewById(R.id.user_single_name);
userNameView.setText(name);
}
public void setUserImage (String thumb_image){
CircleImageView userImageView = (CircleImageView) mView.findViewById(R.id.user_single_image);
Picasso.get().load(thumb_image).placeholder(R.drawable.default_avtar101).into(userImageView);
}
public void setUserOnline (Boolean online_status){
CircleImageView userOnlineView = (CircleImageView) mView.findViewById(R.id.user_single_online);
if (online_status){
userOnlineView.setVisibility(View.VISIBLE);
}
else
userOnlineView.setVisibility(View.INVISIBLE);
}
}
}
logcat如下
2019-10-09 19:36:28.256 9058-9058 / com.rishav.baatcheet E / AndroidRuntime:FATAL EXCEPTION:main 流程:com.rishav.baatcheet,PID:9058 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ java.lang.String java.lang.Object.toString()” 在com.rishav.baatcheet.FreindsFragment $ 1 $ 1.onDataChange(FreindsFragment.java:85) com.google.firebase.database.core.ValueEventRegistration.fireEvent(com.google.firebase:firebase-database @@ 19.1.0:75) com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database @@ 19.1.0:63) com.google.firebase.database.core.view.EventRaiser $ 1.run(com.google.firebase:firebase-database @@ 19.1.0:55) 在android.os.Handler.handleCallback(Handler.java:754) 在android.os.Handler.dispatchMessage(Handler.java:95) 在android.os.Looper.loop(Looper.java:165) 在android.app.ActivityThread.main(ActivityThread.java:6375) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:912) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
数据库JSON如下:
{"Users":{
"client UID":{
"device_token":"cDItwd-LySE",
"name":"Rishav",
"image":"https://firebasestorage.googleapis.com/v0/b...",
"last_seen":1570629990274,
"online":true,
"status":"Default Status",
"thumb_image":"https://firebasestorage.googleapis.com/v0/b..."
},
"another client UID":{
"device_token":"cDItwd-LySE",
"name":"Rishav",
"image":"https://firebasestorage.googleapis.com/v0/b...",
"last_seen":1570629990274,
"online":true,
"status":"Default Status",
"thumb_image":"https://firebasestorage.googleapis.com/v0/b..."
}
},
"Friends":{
"Current client UID":{
"Friend client UID":{
"date":"09-Oct-2019 7:10:34 PM"
}
},
"Friend client UID":{
"Current client UID":{
"date":"09-Oct-2019 7:10:34 PM"
}
}
}}