Java 接收空的可包裹对象

Java 接收空的可包裹对象,java,android,android-fragments,parcelable,Java,Android,Android Fragments,Parcelable,我正在尝试使用parcelable将对象从我的RecyclerView适配器传递到片段。但是,当我尝试接收数据时,bundle为null。我看过其他的例子,但我看不出我错在哪里 可包裹类 public class Country extends BaseObservable implements Parcelable { @SerializedName("name") @Expose private String name; @SerializedName("sn

我正在尝试使用parcelable将对象从我的RecyclerView适配器传递到片段。但是,当我尝试接收数据时,bundle为null。我看过其他的例子,但我看不出我错在哪里

可包裹类

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);

    }

    }

.replace(R.id.frag_容器,新CountryFragment())
应该是

.replace(R.id.frag\u容器,countryFragment)
您正在创建第二个实例,而不是传递设置参数的实例

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();


    }


}
        Country country;
        Bundle bundle = this.getArguments();
        if (bundle != null) {
            country = bundle.getParcelable("Country");
        }