我正尝试将对象列表从活动发送到片段,其中我的对象和对象列表模型扩展了Parcable:
(CurrencyModel)
public class CurrencyModel implements Parcelable
{
@SerializedName("name")
@Expose
private String name;
@SerializedName("slug")
@Expose
private String slug;
@SerializedName("type")
@Expose
private String type;
@SerializedName("unit")
@Expose
private String unit;
@SerializedName("fee_percent")
@Expose
private String feePercent;
@SerializedName("price_usd")
@Expose
private String priceUsd;
@SerializedName("change_24")
@Expose
private String change24;
@SerializedName("min")
@Expose
private String min;
@SerializedName("max")
@Expose
private String max;
@SerializedName("stock")
@Expose
private String stock;
@SerializedName("wallet")
@Expose
private String wallet;
@SerializedName("updated_at")
@Expose
private String updatedAt;
@SerializedName("icon")
@Expose
private String icon;
public final static Parcelable.Creator<CurrencyModel> CREATOR = new Creator<CurrencyModel>() {
@SuppressWarnings({
"unchecked"
})
public CurrencyModel createFromParcel(Parcel in) {
return new CurrencyModel(in);
}
public CurrencyModel[] newArray(int size) {
return (new CurrencyModel[size]);
}
}
;
protected CurrencyModel(Parcel in) {
this.name = ((String) in.readValue((String.class.getClassLoader())));
this.slug = ((String) in.readValue((String.class.getClassLoader())));
this.type = ((String) in.readValue((String.class.getClassLoader())));
this.unit = ((String) in.readValue((String.class.getClassLoader())));
this.feePercent = ((String) in.readValue((String.class.getClassLoader())));
this.priceUsd = ((String) in.readValue((String.class.getClassLoader())));
this.change24 = ((String) in.readValue((String.class.getClassLoader())));
this.min = ((String) in.readValue((String.class.getClassLoader())));
this.max = ((String) in.readValue((String.class.getClassLoader())));
this.stock = ((String) in.readValue((String.class.getClassLoader())));
this.wallet = ((String) in.readValue((String.class.getClassLoader())));
this.updatedAt = ((String) in.readValue((String.class.getClassLoader())));
this.icon = ((String) in.readValue((String.class.getClassLoader())));
}
public CurrencyModel() {
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getSlug() {
return slug;
}
public void setSlug(String slug) {
this.slug = slug;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getUnit() {
return unit;
}
public void setUnit(String unit) {
this.unit = unit;
}
public String getFeePercent() {
return feePercent;
}
public void setFeePercent(String feePercent) {
this.feePercent = feePercent;
}
public String getPriceUsd() {
return priceUsd;
}
public void setPriceUsd(String priceUsd) {
this.priceUsd = priceUsd;
}
public String getChange24() {
return change24;
}
public void setChange24(String change24) {
this.change24 = change24;
}
public String getMin() {
return min;
}
public void setMin(String min) {
this.min = min;
}
public String getMax() {
return max;
}
public void setMax(String max) {
this.max = max;
}
public String getStock() {
return stock;
}
public void setStock(String stock) {
this.stock = stock;
}
public String getWallet() {
return wallet;
}
public void setWallet(String wallet) {
this.wallet = wallet;
}
public String getUpdatedAt() {
return updatedAt;
}
public void setUpdatedAt(String updatedAt) {
this.updatedAt = updatedAt;
}
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(name);
dest.writeValue(slug);
dest.writeValue(type);
dest.writeValue(unit);
dest.writeValue(feePercent);
dest.writeValue(priceUsd);
dest.writeValue(change24);
dest.writeValue(min);
dest.writeValue(max);
dest.writeValue(stock);
dest.writeValue(wallet);
dest.writeValue(updatedAt);
dest.writeValue(icon);
}
public int describeContents() {
return 0;
}
}
和GetCurrenciesModel:
public class GetCurrenciesModel implements Parcelable
{
@SerializedName("status")
@Expose
private Boolean status;
@SerializedName("message")
@Expose
private String message;
@SerializedName("currencies")
@Expose
private List<CurrencyModel> currencies = null;
public final static Parcelable.Creator<GetCurrenciesModel> CREATOR = new Creator<GetCurrenciesModel>() {
@SuppressWarnings({
"unchecked"
})
public GetCurrenciesModel createFromParcel(Parcel in) {
return new GetCurrenciesModel(in);
}
public GetCurrenciesModel[] newArray(int size) {
return (new GetCurrenciesModel[size]);
}
}
;
protected GetCurrenciesModel(Parcel in) {
this.status = ((Boolean) in.readValue((Boolean.class.getClassLoader())));
this.message = ((String) in.readValue((String.class.getClassLoader())));
in.readList(this.currencies, (CurrencyModel.class.getClassLoader()));
}
public GetCurrenciesModel() {
}
public Boolean getStatus() {
return status;
}
public void setStatus(Boolean status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public List<CurrencyModel> getCurrencies() {
return currencies;
}
public void setCurrencies(List<CurrencyModel> currencies) {
this.currencies = currencies;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeValue(status);
dest.writeValue(message);
dest.writeList(currencies);
}
public int describeContents() {
return 0;
}
}
这是我添加要捆绑的货币列表以便将其发送到片段的方法:
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("Currencies", (ArrayList<? extends Parcelable>)currencies);
setData();
Fragment fragment = new FeesFragment();
fragment.setArguments(bundle);
我确定不会为null的货币,因为我烘烤了它的值。 但是,当我尝试从这样的片段中检索该信息时:
currencies = getArguments().getParcelableArrayList("Currencies");
我将得到NullPointerException Erro,因为货币为null。 请帮我解决这个问题。
更新:
错误:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.util.ArrayList android.os.Bundle.getParcelableArrayList(java.lang.String)' on a null object reference
at com.x.x.Fragments.FeesFragment.onCreateView(FeesFragment.java:56)
注意:
FeesFragment也被另一个地方调用。 我正在使用tabView
@Override
public Fragment getItem(int position) {
switch (position){
case 0 :
// Another Fragment
case 1 :
return new FeesFragment();
}
return null;
}
答案 0 :(得分:1)
您的片段没有设置参数。
return new FeesFragment();
这不会使用args软件包设置片段,因此getArguments()
返回null。使用设置参数束的代码来实例化片段。
答案 1 :(得分:-1)
可能是由于敏感性问题;您的@SerializedName(“ currencies”)以'c'开头,并且您正在尝试使用“ Currencies”从分发包中获取Parcelable ArrayList。