这是我收到的错误消息
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.authenticatorapp.MessageActivity.<init>(MessageActivity.java:36)
这是36行显示的代码行
String recipientId = getIntent().getStringExtra("recipientId");
DocumentReference recipientReference = FirebaseFirestore.getInstance().collection("users").document(recipientId);
这是上述getStringExtra()的putStringExtra()
@Override
protected void onBindViewHolder(@NonNull ViewHolder holder, int position, @NonNull final User model) {
holder.fName.setText(model.getfName());
holder.profile_image.setImageResource(R.mipmap.ic_launcher);
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext(), MessageActivity.class);
intent.putExtra("recipientId", model.getUserId());
v.getContext().startActivity(intent);
}
});
}
这将是我使用的收件人参考
recipientReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if (task.isSuccessful()) {
DocumentSnapshot document = task.getResult();
if (document.exists()) {
Log.d(TAG, "DocumentSnapshot data: " + document.getData());
User user = document.toObject(User.class);
username.setText(user.getfName());
} else {
Log.d(TAG, "No such document");
}
} else {
Log.d(TAG, "get failed with ", task.getException());
}
}
});