构造函数不能应用于给定类型;要求:包裹;实际和正式的参数列表长度不同

时间:2017-07-18 10:49:41

标签: android builder lombok parcel

我正在使用Android Studio中的某个应用,并希望通过意图将数据从我的城市实例传输到下一个活动。为此,我使用Plugin" Android Parcelable代码生成器"生成了Parcelable代码。

现在我在编译期间收到以下错误消息:

Error:(19, 1) error: constructor City in class City cannot be applied to given types;
    required: Parcel
    found: boolean,String,int[],LatLng
    reason: actual and formal argument lists differ in length

城市课程:

import android.os.Parcel;
import android.os.Parcelable;

import com.google.android.gms.maps.model.LatLng;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@EqualsAndHashCode
@Builder(builderMethodName = "hiddenBuilder")
@ToString
public class City implements Parcelable {

    private boolean isSolved;
    private final String name;
    private int[] imageIds;
    private LatLng latLng;

    public static CityBuilder builder(String name) {
        return hiddenBuilder().name(name);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(this.isSolved ? (byte) 1 : (byte) 0);
        dest.writeString(this.name);
        dest.writeIntArray(this.imageIds);
        dest.writeParcelable(this.latLng, flags);
    }

    protected City(Parcel in) {
        this.isSolved = in.readByte() != 0;
        this.name = in.readString();
        this.imageIds = in.createIntArray();
        this.latLng = in.readParcelable(LatLng.class.getClassLoader());
    }

    public static final Parcelable.Creator<City> CREATOR = new Parcelable.Creator<City>() {
        @Override
        public City createFromParcel(Parcel source) {
            return new City(source);
        }

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

需要做些什么才能让Lombok的Builder Annotation与Parcel一起使用?

3 个答案:

答案 0 :(得分:0)

来自Reena的提示与POJO中的所有arg构造函数使它工作。有没有人解释为什么这是必要的?

Btw:LatLng不可序列化。我不得不用自己的包装纸交换它。

所有arg构造函数的完整示例:

import android.os.Parcel;
import android.os.Parcelable;

import com.my.project.Coordinate;

import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Setter
@Getter
@EqualsAndHashCode
@Builder(builderMethodName = "hiddenBuilder")
@ToString
public class City implements Parcelable {

    private boolean isSolved;
    private final String name;
    private int[] imageIds;
    private Coordinate coordinate;

    public City(final boolean isSolved, final String name,
                final int[] imageIds, final Coordinate coordinate) {
        this.isSolved = isSolved;
        this.name = name;
        this.imageIds = imageIds;
        this.coordinate = coordinate;
    }

    public static CityBuilder builder(String name) {
        return hiddenBuilder().name(name);
    }

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

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeByte(this.isSolved ? (byte) 1 : (byte) 0);
        dest.writeString(this.name);
        dest.writeIntArray(this.imageIds);
        dest.writeParcelable(this.coordinate, flags);
    }

    protected City(Parcel in) {
        this.isSolved = in.readByte() != 0;
        this.name = in.readString();
        this.imageIds = in.createIntArray();
        this.coordinate = in.readParcelable(Coordinate.class.getClassLoader());
    }

    public static final Parcelable.Creator<City> CREATOR = new Parcelable.Creator<City>() {
        @Override
        public City createFromParcel(Parcel source) {
            return new City(source);
        }

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

答案 1 :(得分:0)

在POJO中添加boolean,String,int [],LatLng的参数化构造函数,您可以参考以下代码:

 public City(boolean isSolved, String name,int[] imageIds,LatLng  latLng) {
        this.isSolved = isSolved;
        this.name = name;
        this.imageIds = imageIds;
        this.latLng = latLng;
    }

答案 2 :(得分:0)

您可能需要城市班的建设者先生

public class City implements Parcelable {

    private boolean isSolved;
    private final String name;
    private int[] imageIds;
    private LatLng latLng;

    .....................
    public City(){
    // You can leave it blank
    }
    .....................
}