安装 Project Lombok 后处理 RealmObject 后代时出错
Error while processing RealmObject descedant after installing Project Lombok
那是我要存储在我的 Realm 数据库中的 class。在构建应用程序时它会抛出 "Error:A default public constructor with no argument must be declared if a custom constructor is declared."
但在使用领域之前没问题 - 不需要默认构造函数。我也收到了这样的警告: Warning:(18, 1) Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
How to fix errors?
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import io.realm.RealmObject;
import kg.zuber.aqe_android.R;
import lombok.Data;
import lombok.Getter;
@Data
public class Measure extends RealmObject {
@Getter
int id;
@NonNull
String name;
@Getter
@NonNull
String slug;
public Measure(int id, @NonNull String name, @NonNull String slug) {
super();
this.id = id;
this.name = name;
this.slug = slug;
}
@Override
public String toString() {
return this.name;
}
public static class MeasureAdapter extends ArrayAdapter<Measure> {
private final Context mContext;
private ArrayList<Measure> measures;
public MeasureAdapter(Context context, ArrayList<Measure> mMeasures) {
super(context, R.layout.blue_spinner_item, mMeasures);
this.mContext = context;
measures = mMeasures;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Measure device = measures.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.blue_spinner_item, parent, false);
}
TextView textView = (TextView)convertView.findViewById(R.id.text);
textView.setText(device.name);
return convertView;
}
}
}
除了 @Data
,您还可以使用以下内容:
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper=false)
并删除手写的构造函数。
此外,您不需要 @Getter
注释。如果你想让字段被空检查,你应该使用 @lombok.NonNull
或导入它而不是 android.support.annotation.NonNull
.
披露:我是 lombok 开发人员。
那是我要存储在我的 Realm 数据库中的 class。在构建应用程序时它会抛出 "Error:A default public constructor with no argument must be declared if a custom constructor is declared."
但在使用领域之前没问题 - 不需要默认构造函数。我也收到了这样的警告: Warning:(18, 1) Generating equals/hashCode implementation but without a call to superclass, even though this class does not extend java.lang.Object. If this is intentional, add '@EqualsAndHashCode(callSuper=false)' to your type.
How to fix errors?
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import io.realm.RealmObject;
import kg.zuber.aqe_android.R;
import lombok.Data;
import lombok.Getter;
@Data
public class Measure extends RealmObject {
@Getter
int id;
@NonNull
String name;
@Getter
@NonNull
String slug;
public Measure(int id, @NonNull String name, @NonNull String slug) {
super();
this.id = id;
this.name = name;
this.slug = slug;
}
@Override
public String toString() {
return this.name;
}
public static class MeasureAdapter extends ArrayAdapter<Measure> {
private final Context mContext;
private ArrayList<Measure> measures;
public MeasureAdapter(Context context, ArrayList<Measure> mMeasures) {
super(context, R.layout.blue_spinner_item, mMeasures);
this.mContext = context;
measures = mMeasures;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Measure device = measures.get(position);
if (convertView == null) {
convertView = LayoutInflater.from(getContext())
.inflate(R.layout.blue_spinner_item, parent, false);
}
TextView textView = (TextView)convertView.findViewById(R.id.text);
textView.setText(device.name);
return convertView;
}
}
}
除了 @Data
,您还可以使用以下内容:
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(callSuper=false)
并删除手写的构造函数。
此外,您不需要 @Getter
注释。如果你想让字段被空检查,你应该使用 @lombok.NonNull
或导入它而不是 android.support.annotation.NonNull
.
披露:我是 lombok 开发人员。