英文:
Debugger shows null value despite having value in Firebase Database
问题
这是代码的翻译部分:
ProfileInfo.class
public class ProfileInfo {
String userName, profilePhotoUrl, phoneNumber, userId;
public ProfileInfo() {}
public ProfileInfo(String userName, String profilePhotoUrl, String phoneNumber, String userId) {
this.userName = userName;
this.profilePhotoUrl = profilePhotoUrl;
this.phoneNumber = phoneNumber;
this.userId = userId;
}
public ProfileInfo(String userName, String phoneNumber) {
this.userName = userName;
this.phoneNumber = phoneNumber;
}
public String getUserId() {
return userId;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getUserName() {
return userName;
}
public String getProfilePhotoUrl() {
return profilePhotoUrl;
}
}
UserAdapter.class
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {
private ArrayList<ProfileInfo> minfo;
public static final String USER_ID = "userid";
Context context;
public UsersAdapter(Context context, ArrayList<ProfileInfo> minfo) {
this.context = context;
this.minfo = minfo;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final ProfileInfo profileInfo = minfo.get(position);
holder.username.setText(profileInfo.getUserName());
holder.phoneNumber.setText(profileInfo.getPhoneNumber());
holder.id.setText(profileInfo.getUserId());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MessageLayout.class);
intent.putExtra(USER_ID, profileInfo.getUserId());
Log.d("UserID:", profileInfo.getUserId());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return minfo.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView username;
TextView phoneNumber, id;
CircleImageView profileImage;
RelativeLayout relativeLayout;
ViewHolder(@NonNull View itemView) {
super(itemView);
username = itemView.findViewById(R.id.user);
phoneNumber = itemView.findViewById(R.id.phone);
id = itemView.findViewById(R.id.id);
profileImage = itemView.findViewById(R.id.circleProfileImage);
relativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
}
MessageLayout.class
public class MessageLayout extends AppCompatActivity {
FirebaseUser currentUser;
DatabaseReference db, chatDb;
EditText message;
ImageButton sendMessage;
TextView username;
CircleImageView profileImage;
String recieverId, messageText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_layout);
message = findViewById(R.id.editTextMessage);
sendMessage = findViewById(R.id.sendMessage);
username = findViewById(R.id.displayUsername);
profileImage = findViewById(R.id.circledp);
currentUser = FirebaseAuth.getInstance().getCurrentUser();
Intent intent = getIntent();
recieverId = intent.getStringExtra(UsersAdapter.USER_ID);
db = FirebaseDatabase.getInstance().getReference().child("UserInfo").child(recieverId);
chatDb = FirebaseDatabase.getInstance().getReference().child("Chats");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ProfileInfo info = dataSnapshot.getValue(ProfileInfo.class);
username.setText(info.getUserName());
Glide.with(MessageLayout.this).load(info.getProfilePhotoUrl()).into(profileImage);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
英文:
I was trying to send a value through intent, but my app was getting crashed. So, I try to check the value in the debugger and it is showing null. Here is the image.
As you can see that it shows userId = "null"
and profilephotoURl ="null"
. Why is this happening?
And here is the image of the Database.
Here is the Code
ProfileInfo.class
public class ProfileInfo {
String userName, profilePhotoUrl, phoneNumber, userId;
public ProfileInfo() {}
public ProfileInfo(String userName, String profilePhotoUrl, String phoneNumber, String userId) {
this.userName = userName;
this.profilePhotoUrl = profilePhotoUrl;
this.phoneNumber = phoneNumber;
this.userId = userId;
}
public ProfileInfo(String userName, String phoneNumber) {
this.userName = userName;
this.phoneNumber = phoneNumber;
}
public String getUserId() {
return userId;
}
public String getPhoneNumber() {
return phoneNumber;
}
public String getUserName() {
return userName;
}
public String getProfilePhotoUrl() {
return profilePhotoUrl;
}
UserAdapter.class
public class UsersAdapter extends RecyclerView.Adapter<UsersAdapter.ViewHolder> {
private ArrayList<ProfileInfo> minfo;
public static final String USER_ID = "userid";
Context context;
public UsersAdapter(Context context, ArrayList<ProfileInfo> minfo) {
this.context = context;
this.minfo = minfo;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_item, parent, false);
RecyclerView.LayoutParams lp = new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(lp);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position) {
final ProfileInfo profileInfo = minfo.get(position);
holder.username.setText(profileInfo.getUserName());
holder.phoneNumber.setText(profileInfo.getPhoneNumber());
holder.id.setText(profileInfo.getUserId());
holder.itemView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(context, MessageLayout.class);
intent.putExtra(USER_ID, profileInfo.getUserId());
Log.d("UserID:", profileInfo.getUserId());
context.startActivity(intent);
}
});
}
@Override
public int getItemCount() {
return minfo.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
TextView username;
TextView phoneNumber, id;
CircleImageView profileImage;
RelativeLayout relativeLayout;
ViewHolder(@NonNull View itemView) {
super(itemView);
username = itemView.findViewById(R.id.user);
phoneNumber = itemView.findViewById(R.id.phone);
id = itemView.findViewById(R.id.id);
profileImage = itemView.findViewById(R.id.circleProfileImage);
relativeLayout = itemView.findViewById(R.id.relativeLayout);
}
}
}
MessageLayout.class
public class MessageLayout extends AppCompatActivity {
FirebaseUser currentUser;
DatabaseReference db, chatDb;
EditText message;
ImageButton sendMessage;
TextView username;
CircleImageView profileImage;
String recieverId, messageText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_message_layout);
message = findViewById(R.id.editTextMessage);
sendMessage = findViewById(R.id.sendMessage);
username = findViewById(R.id.displayUsername);
profileImage = findViewById(R.id.circledp);
currentUser = FirebaseAuth.getInstance().getCurrentUser();
Intent intent = getIntent();
recieverId = intent.getStringExtra(UsersAdapter.USER_ID);
db = FirebaseDatabase.getInstance().getReference().child("UserInfo").child(recieverId);
chatDb = FirebaseDatabase.getInstance().getReference().child("Chats");
db.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
ProfileInfo info = dataSnapshot.getValue(ProfileInfo.class);
username.setText(info.getUserName());
Glide.with(MessageLayout.this).load(info.getProfilePhotoUrl()).into(profileImage);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
答案1
得分: 0
在您的模型类中,应该有一个带参数的单参数构造函数。尝试将第二个带参数的构造函数注释掉,看看是否有帮助。
英文:
In you model class, there should be a single parameterized constructor.Try commenting the second parameterized constructor and see if that helps.
答案2
得分: 0
问题出在你的构造函数中。看起来你的datasnapshot正在使用你的ProfileInfo(String userName, String phoneNumber)
构造函数进行强制类型转换。
你可以这样做:确保你的模型类中只有两个构造函数:常规的无参构造函数,然后是你的完整构造函数。类似这样:
public ProfileInfo() {}
public ProfileInfo(String userName, String profilePhotoUrl, String phoneNumber, String userId) {
this.userName = userName;
this.profilePhotoUrl = profilePhotoUrl;
this.phoneNumber = phoneNumber;
this.userId = userId;
}
然后,运行你的代码,再试一次。
英文:
The problem is in your constructor. It seems like your datasnapshot is being cast using your ProfileInfo(String userName, String phoneNumber)
constructor.
Here's what you can do: Ensure that there are only two constructors in your model class: The regular no-argument constructor and then your full-blown constructor. Something like this:
public ProfileInfo() {}
public ProfileInfo(String userName, String profilePhotoUrl, String phoneNumber, String userId) {
this.userName = userName;
this.profilePhotoUrl = profilePhotoUrl;
this.phoneNumber = phoneNumber;
this.userId = userId;
}
Then, run your code and try again.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论