android studioで開発中のアプリをConvert Java File to Kotlin Fileで変換したところうまく動かなくなりました。
エラーが発生しているのはRetrofitで使うデータクラスの部分です。
Parcelableはプラグインを使って作成しました。
Java
1public class Word implements Parcelable { 2//一部抜粋 3 @Expose 4 private Integer id; 5 @Expose 6 private String keyword; 7 @Expose 8 private Integer type; 9 @Expose 10 private Integer level; 11 @Expose 12 private String summary; 13 @Expose 14 private String description; 15 @Expose 16 private boolean nonfiction; 17 @Expose 18 private boolean fiction; 19 20 21//Getter, Setterは省略 22 23 @Override 24 public int describeContents() { 25 return 0; 26 } 27 28 @Override 29 public void writeToParcel(Parcel dest, int flags) { 30 dest.writeValue(this.id); 31 dest.writeString(this.keyword); 32 dest.writeValue(this.type); 33 dest.writeValue(this.level); 34 dest.writeString(this.summary); 35 dest.writeString(this.description); 36 dest.writeByte(this.nonfiction ? (byte) 1 : (byte) 0); 37 dest.writeByte(this.fiction ? (byte) 1 : (byte) 0); 38 } 39 40 public Word() { 41 } 42 43 protected Word(Parcel in) { 44 this.id = (Integer) in.readValue(Integer.class.getClassLoader()); 45 this.keyword = in.readString(); 46 this.type = (Integer) in.readValue(Integer.class.getClassLoader()); 47 this.level = (Integer) in.readValue(Integer.class.getClassLoader()); 48 this.summary = in.readString(); 49 this.description = in.readString(); 50 this.nonfiction = in.readByte() != 0; 51 this.fiction = in.readByte() != 0; 52 } 53 54 public static final Creator<Word> CREATOR = new Creator<Word>() { 55 @Override 56 public Word createFromParcel(Parcel source) { 57 return new Word(source); 58 } 59 60 @Override 61 public Word[] newArray(int size) { 62 return new Word[size]; 63 } 64 }; 65}
これをConvert Java File to Kotlin Fileで変換すると赤波線が出ます。
上記のコードをKotlinで記述するにはどうすればいいのか教えてください。
変換後のコードは下記。
Kotlin
1//Kotlin 2class Word : Parcelable { 3 @Expose 4 var id: Int? = null 5 @Expose 6 var keyword: String? = null 7 @Expose 8 var type: Int? = null 9 @Expose 10 var level: Int? = null 11 @Expose 12 var summary: String? = null 13 @Expose 14 var description: String? = null 15 @Expose 16 var isNonfiction: Boolean = false 17 @Expose 18 var isFiction: Boolean = false 19 20 override fun describeContents(): Int { 21 return 0 22 } 23 24 override fun writeToParcel(dest: Parcel, flags: Int) { 25 dest.writeValue(this.id) 26 dest.writeString(this.keyword) 27 dest.writeValue(this.type) 28 dest.writeValue(this.level) 29 dest.writeString(this.summary) 30 dest.writeString(this.description) 31 dest.writeByte(if (this.isNonfiction) 1.toByte() else 0.toByte()) 32 dest.writeByte(if (this.isFiction) 1.toByte() else 0.toByte()) 33 } 34 35 constructor() {} 36 37 protected constructor(`in`: Parcel) { 38 this.id = `in`.readValue(Int::class.java.classLoader) as Int 39 this.keyword = `in`.readString() 40 this.type = `in`.readValue(Int::class.java.classLoader) as Int 41 this.level = `in`.readValue(Int::class.java.classLoader) as Int 42 this.summary = `in`.readString() 43 this.description = `in`.readString() 44 this.isNonfiction = `in`.readByte().toInt() != 0 45 this.isFiction = `in`.readByte().toInt() != 0 46 } 47 companion object { 48 49 @JvmField 50 val CREATOR: Parcelable.Creator<Word> = object : Parcelable.Creator<Word> { 51 override fun createFromParcel(source: Parcel): Word { 52 return Word(source) 53 } 54 override fun newArray(size: Int): Array<Word?> { 55 return arrayOfNulls(size) 56 } 57 } 58 } 59}
エラーメッセージ
Caused by: android.os.BadParcelableException: Parcelable protocol requires a Parcelable.Creator object called CREATOR on class com.example.myapplication.Word
意味が分からなかったらすみません。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/03/31 02:42
2017/03/31 02:43