调试器显示空值,尽管在Firebase数据库中有值。

huangapple go评论83阅读模式
英文:

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.

调试器显示空值,尽管在Firebase数据库中有值。

As you can see that it shows userId = &quot;null&quot; and profilephotoURl =&quot;null&quot;. Why is this happening?

And here is the image of the Database.

调试器显示空值,尽管在Firebase数据库中有值。

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&lt;UsersAdapter.ViewHolder&gt; {
private ArrayList&lt;ProfileInfo&gt; minfo;
public static final String USER_ID = &quot;userid&quot;;
Context context;
public UsersAdapter(Context context, ArrayList&lt;ProfileInfo&gt; 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(&quot;UserID:&quot;, 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(&quot;UserInfo&quot;).child(recieverId);
chatDb = FirebaseDatabase.getInstance().getReference().child(&quot;Chats&quot;);
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.

huangapple
  • 本文由 发表于 2020年5月29日 16:54:26
  • 转载请务必保留本文链接:https://go.coder-hub.com/62082141.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定