gpt4 book ai didi

java - 来自用户模型的 ArrayList 返回 null

转载 作者:行者123 更新时间:2023-11-29 02:37:54 25 4
gpt4 key购买 nike

所以在我正在创建的应用程序中,我有一个定义如下的用户类

public class User implements Serializable{

private int id;
private String username;
private String firstName;
private String lastName;
private String email;
private String password;
private String picture;
private ArrayList<String> tags;
private double rating;
private Category favorite;
private boolean star;
private Location homeLocation;


public User(int id, String username, String firstName,String lastName,String picture, ArrayList<String> tags,double rating, Category favorite, boolean star,Location homeLocation,String email,String password) {
this.id = id;
this.username = username;
this.firstName = firstName;
this.lastName = lastName;
this.picture = picture;
this.tags = tags;
this.rating = rating;
this.favorite = favorite;
this.star = star;
this.homeLocation = homeLocation;
this.email = email;
this.password = password;

}

public int getId() {
return id;
}

public String getUsername() {
return username;
}

public String getFirstName() {
return firstName;
}

public String getLastName(){ return lastName;}

public String getPicture(){ return picture;}

public ArrayList<String> getTags() {
return tags;
}

public double getRating(){ return rating;}

public Category getFavorite(){ return favorite;}

public Boolean getStar(){ return star;}

public Location getHomeLocation(){return homeLocation;}

public String getEmail(){return email;}

public String getPassword(){return password;}

public void setId(int id){

this.id = id;

}

现在,当我创建一个用户对象时,我传递一个 ArrayList<String>标记,这些标记在记录时会按预期显示。

List<String> listTags = Arrays.asList(DummyTags.tagsNewUser);
ArrayList<String> tags = new ArrayList<>();

tags.addAll(listTags);

Log.d("Tags",tags.toString());

user = new User(0,userName,firstName,lastName,picturePlaceHolder,tags,4,category,true,loc,email,password);

但是,当我在其中使用此用户对象时,例如个人资料页面,并通过类中的 getter 检索用户标签时,它始终返回 null。

user = (User) getArguments().getSerializable("User");

tags = user.getTags();

这样做总是让标签为空,不管是什么ArrayList我在创建用户对象时通过。

我还在 android studio 中收到一条警告,说正在调用 user.getTags()可能会产生 npe。用户模型的每个其他部分都工作得很好。我已经尝试解决这个问题一段时间了,但未能找到与我的问题相关的任何信息,因此我们将不胜感激!

最佳答案

尝试:

    Bundle bundle=new Bundle();
bundle.putStringArrayList("tags",tags);
bundle.putSerializable("User",user);

然后检索为:

tags = getArguments().getStringArrayList("tags");

您的解决方案不起作用的原因是 ArrayList 没有实现 Serializable 接口(interface)

关于java - 来自用户模型的 ArrayList 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45958247/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com