因此,在我正在创建的应用程序中,我将用户类定义为以下
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;
}
现在,当我创建一个User对象时,我会传递一个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
,执行此操作的标记始终为null。
我也在android studio中收到警告,说调用user.getTags()
可能会产生一个npe。用户模型的每个其他部分都可以正常工作。我已经尝试解决这个问题一段时间了,并且无法找到任何与我的问题有关的内容,所以任何帮助都将不胜感激!
答案 0 :(得分:0)
尝试:
Bundle bundle=new Bundle();
bundle.putStringArrayList("tags",tags);
bundle.putSerializable("User",user);
然后检索为:
tags = getArguments().getStringArrayList("tags");
您的解决方案不起作用的原因是ArrayList没有实现Serializable接口