2017-07-18 54 views
0

我正在開發Android Studio中的應用程序,並且希望將數據從我的城市實例傳輸到下一個活動。爲此,我使用Plugin「Android Parcelable code generator」生成了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]; 
     } 
    }; 
} 

需要採取哪些措施,使龍目島與包裹builder註釋工作?

+0

可以嘗試在POJO中添加布爾型,String,int [],LatLng參數化構造函數? – Reena

+0

添加建設者類請 –

+0

@Oussema Aroua:建設者類是由lombok生成的。 –

回答

0

添加參數的構造函數與布爾型,字符串,INT [],經緯度在你的POJO,你可以參考下面的代碼:

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

來自Reena的提示和POJO中的所有參數構造函數使它工作。有沒有人有解釋爲什麼這是必要的?

Btw:LatLng不可串行化。我不得不用自己的包裝來交換它。

所有參數的構造完整的示例:

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]; 
     } 
    }; 
} 
+0

它的優點需要思考 – Reena

相關問題