gpt4 book ai didi

java - 使用带有扩展名的自动值时,AutoValue : Missing required properties,

转载 作者:行者123 更新时间:2023-12-01 09:04:16 26 4
gpt4 key购买 nike

我使用 AutoValueauto-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$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)

最佳答案

您不应该自己实现 wither 方法。只需将其声明为抽象,然后让 auto-value-with 完成其工作即可。

public abstract FeedItem withTotalFlags(int totalFlags);
<小时/>

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

abstract Builder toBuilder();

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

public FeedItem withTotalFlags(int totalFlags) {
return toBuilder().setTotalFlags(totalFlags).build();
}

关于java - 使用带有扩展名的自动值时,AutoValue : Missing required properties,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41415620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com