0

我正在使用AutoValuewith auto-value-withextension 创建一个稍微改变的对象。

我的课是:

@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$1.onResponse(FeedRepository.java:72)
         at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.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)
4

1 回答 1

1

您不应该自己实现枯萎方法。只需将其声明为抽象并让其auto-value-with完成工作。

public abstract FeedItem withTotalFlags(int totalFlags);

或者,您可以toBuilder()为您的模型声明一个方法并使用它,而不是创建一个新的构建器。

abstract Builder toBuilder();

它将预填充原始对象中的所有值并解决您的错误。

public FeedItem withTotalFlags(int totalFlags) {
    return toBuilder().setTotalFlags(totalFlags).build();
}
于 2017-01-06T15:22:44.880 回答