AutoValue:缺少必需的属性,当使用带有扩展名的自动值时
AutoValue : Missing required properties, when using auto-value-with extension
我正在使用 AutoValue
和 auto-value-with
扩展来创建一个稍微改变的对象。
我的 class 是:
@AutoValue
public abstract class FeedItem implements Parcelable {
public abstract String getAccountId();
public abstract String getName();
public abstract String getCompany();
@Nullable
public abstract String getAvatar();
@Nullable
public abstract String getPostImage();
@Nullable
public abstract String getThumb();
public abstract String getPostId();
public abstract String getFlaggedYes();
public abstract int getTotalFlags();
public abstract int getTotalComments();
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
public static Builder builder() {
return new AutoValue_FeedItem.Builder();
}
@AutoValue.Builder
public static abstract class Builder {
public abstract Builder setAccountId(String accountId);
public abstract Builder setName(String name);
public abstract Builder setCompany(String company);
public abstract Builder setAvatar(@Nullable String avatar);
public abstract Builder setPostImage(@Nullable String image);
public abstract Builder setThumb(@Nullable String thumb);
public abstract Builder setPostId(String postId);
public abstract Builder setFlaggedYes(String flaggedYes);
public abstract Builder setTotalFlags(int totalFlags);
public abstract Builder setTotalComments(int totalComments);
public abstract FeedItem build();
}
}
在哪里可以看到我正在使用:
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
创建修改后的对象。
问题是,当我尝试像这样使用它时:
FeedItem feedItem = dataset.get(position).withTotalFlags(updatedTotalFlags);
我得到:
01-01 14:49:00.560 23369-23369/MY_PACKAGE E/AndroidRuntime: FATAL EXCEPTION: main
Process: MY_PACKAGE, PID: 23369
java.lang.IllegalStateException: Missing required properties: accountId name company postId flaggedYes totalComments
at MY_PACKAGE.data.models.$AutoValue_FeedItem$Builder.build($AutoValue_FeedItem.java:275)
at MY_PACKAGE.data.models.FeedItem.withTotalFlags(FeedItem.java:31)
at MY_PACKAGE.main.fragments.feed.adapter.FeedAdapter.updateItem(FeedAdapter.java:247)
at MY_PACKAGE.main.fragments.feed.FeedFragment.updateFlagState(FeedFragment.java:232)
at MY_PACKAGE.main.fragments.feed.FeedPresenter.onFlagStateRequestSuccess(FeedPresenter.java:97)
at MY_PACKAGE.main.fragments.feed.FeedRepository.onResponse(FeedRepository.java:72)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
你不应该自己实现凋零方法。只需声明它是抽象的,让 auto-value-with
完成它的工作。
public abstract FeedItem withTotalFlags(int totalFlags);
或者您可以为您的模型声明一个 toBuilder()
方法并使用它而不是创建一个新的构建器。
abstract Builder toBuilder();
它将预填充原始对象的所有值并解决您的错误。
public FeedItem withTotalFlags(int totalFlags) {
return toBuilder().setTotalFlags(totalFlags).build();
}
我正在使用 AutoValue
和 auto-value-with
扩展来创建一个稍微改变的对象。
我的 class 是:
@AutoValue
public abstract class FeedItem implements Parcelable {
public abstract String getAccountId();
public abstract String getName();
public abstract String getCompany();
@Nullable
public abstract String getAvatar();
@Nullable
public abstract String getPostImage();
@Nullable
public abstract String getThumb();
public abstract String getPostId();
public abstract String getFlaggedYes();
public abstract int getTotalFlags();
public abstract int getTotalComments();
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
public static Builder builder() {
return new AutoValue_FeedItem.Builder();
}
@AutoValue.Builder
public static abstract class Builder {
public abstract Builder setAccountId(String accountId);
public abstract Builder setName(String name);
public abstract Builder setCompany(String company);
public abstract Builder setAvatar(@Nullable String avatar);
public abstract Builder setPostImage(@Nullable String image);
public abstract Builder setThumb(@Nullable String thumb);
public abstract Builder setPostId(String postId);
public abstract Builder setFlaggedYes(String flaggedYes);
public abstract Builder setTotalFlags(int totalFlags);
public abstract Builder setTotalComments(int totalComments);
public abstract FeedItem build();
}
}
在哪里可以看到我正在使用:
public FeedItem withTotalFlags(int totalFlags) {
return builder().setTotalFlags(totalFlags).build();
}
创建修改后的对象。
问题是,当我尝试像这样使用它时:
FeedItem feedItem = dataset.get(position).withTotalFlags(updatedTotalFlags);
我得到:
01-01 14:49:00.560 23369-23369/MY_PACKAGE E/AndroidRuntime: FATAL EXCEPTION: main
Process: MY_PACKAGE, PID: 23369
java.lang.IllegalStateException: Missing required properties: accountId name company postId flaggedYes totalComments
at MY_PACKAGE.data.models.$AutoValue_FeedItem$Builder.build($AutoValue_FeedItem.java:275)
at MY_PACKAGE.data.models.FeedItem.withTotalFlags(FeedItem.java:31)
at MY_PACKAGE.main.fragments.feed.adapter.FeedAdapter.updateItem(FeedAdapter.java:247)
at MY_PACKAGE.main.fragments.feed.FeedFragment.updateFlagState(FeedFragment.java:232)
at MY_PACKAGE.main.fragments.feed.FeedPresenter.onFlagStateRequestSuccess(FeedPresenter.java:97)
at MY_PACKAGE.main.fragments.feed.FeedRepository.onResponse(FeedRepository.java:72)
at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall.run(ExecutorCallAdapterFactory.java:68)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
你不应该自己实现凋零方法。只需声明它是抽象的,让 auto-value-with
完成它的工作。
public abstract FeedItem withTotalFlags(int totalFlags);
或者您可以为您的模型声明一个 toBuilder()
方法并使用它而不是创建一个新的构建器。
abstract Builder toBuilder();
它将预填充原始对象的所有值并解决您的错误。
public FeedItem withTotalFlags(int totalFlags) {
return toBuilder().setTotalFlags(totalFlags).build();
}