Error while processing RealmObject descedant after installing Project Lombok

匿名 (未验证) 提交于 2019-12-03 10:10:24

问题:

That is my class that I want to store in my Realm database. While building an app it throws "Error:A default public constructor with no argument must be declared if a custom constructor is declared." But before using realm it was ok - default constructor was not required. Also I got warnings like that: 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;         }     } } 

回答1:

Instead of only @Data, you can use the following:

@Data @AllArgsConstructor @NoArgsConstructor @EqualsAndHashCode(callSuper=false) 

And remove the hand written constructor.

Also, you don't need the @Getter annotations. If you want to have the fields be null-checked, you should use @lombok.NonNull or import it instead of android.support.annotation.NonNull.

Disclosure: I am a lombok developer.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!