接收空 Parcelable 对象 Android

Receiving null Parcelable object Android

我正在尝试使用 parcelable 将一个对象从我的 RecyclerView 适配器传递到一个片段。但是,当我尝试接收数据时,包为空。我查看了其他示例,但看不出哪里出错了。

可包裹class

public class Country extends BaseObservable implements Parcelable {
    @SerializedName("name")
    @Expose
    private String name;
    @SerializedName("snippet")
    @Expose
    private String snippet;
    @SerializedName("country_id")
    @Expose
    private String countryId;
    @SerializedName("id")
    @Expose
    private String id;
    @SerializedName("coordinates")
    @Expose
    private Coordinates coordinates;
    @SerializedName("images")
    @Expose
    private List<CountryImage> images;

    protected Country(Parcel in) {
        name = in.readString();
        snippet = in.readString();
    }

    public static final Creator<Country> CREATOR = new Creator<Country>() {
        @Override
        public Country createFromParcel(Parcel in) {
            return new Country(in);
        }

        @Override
        public Country[] newArray(int size) {
            return new Country[size];
        }
    };

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(name);
        dest.writeString(snippet);

    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Bindable
    public String getCountryId() {
        return countryId;
    }

    public void setCountryId(String countryId) {
        this.countryId = countryId;
        notifyPropertyChanged(BR.countryId);

    }

    @Bindable
    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
        notifyPropertyChanged(BR.id);

    }

    @Bindable
    public Coordinates getCoordinates() {
        return coordinates;
    }

    public void setCoordinates(Coordinates coordinates) {
        this.coordinates = coordinates;
        notifyPropertyChanged(BR.coordinates);

    }

    @Bindable
    public List<CountryImage> getImages() {
        return images;
    }

    public void setImages(List<CountryImage> images) {
        this.images = images;
        notifyPropertyChanged(BR.images);

    }

    @Bindable
    public String getSnippet() {
        return snippet;
    }

    public void setSnippet(String snippet) {
        this.snippet = snippet;
        notifyPropertyChanged(BR.snippet);

    }

    @Bindable
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
        notifyPropertyChanged(BR.name);

    }

    }

RetrofitAdapter.java

public class RetrofitAdapter extends RecyclerView.Adapter<RetrofitAdapter.MyViewHolder> implements CustomClickListener {
    private List<Country> cities;
    private CustomClickListener customClickListener;
    private View view;


    @NonNull
    @Override
    public MyViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {

        RetroItemBinding retroItemBinding =
                DataBindingUtil.inflate(LayoutInflater.from(viewGroup.getContext()),
                        R.layout.retro_item, viewGroup, false);

        view = viewGroup;

        return new MyViewHolder(retroItemBinding);
    }

    @Override
    public void onBindViewHolder(@NonNull MyViewHolder myViewHolder, int i) {
        myViewHolder.bindTo(cities.get(i));
        myViewHolder.retroItemBinding.setItemClickListener(this);
    }

    @Override
    public int getItemCount() {
        if (cities != null) {
            return cities.size();
        } else {
            return 0;
        }
    }

    public void setCityList(ArrayList<Country> cities) {
        this.cities = cities;
        notifyDataSetChanged();
    }

    class MyViewHolder extends RecyclerView.ViewHolder {

        private RetroItemBinding retroItemBinding;

        public MyViewHolder(@NonNull RetroItemBinding retroItemBinding) {
            super(retroItemBinding.getRoot());

            this.retroItemBinding = retroItemBinding;
        }

        void bindTo(Country country) {
            retroItemBinding.setVariable(BR.city, country);
            retroItemBinding.setVariable(BR.itemClickListener, customClickListener);

            retroItemBinding.executePendingBindings();

        }
    }

    public void cardClicked(Country country) {
        CountryFragment countryFragment = new CountryFragment();
        Bundle bundle = new Bundle();
        bundle.putParcelable("Country", country);
        countryFragment.setArguments(bundle);

       ((FragmentActivity) view.getContext()).getSupportFragmentManager().beginTransaction()
                .replace(R.id.frag_container, new CountryFragment())
                .commit();


    }


}

我在 CountryFragment.java

中收到尝试接收数据的地方
        Country country;
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            country = bundle.getParcelable("Country");
        }
.replace(R.id.frag_container, new CountryFragment())

应该是

.replace(R.id.frag_container, countryFragment)

您正在创建第二个实例,而不是传递您设置参数的实例。