Android Studio:房间:错误:找不到字段的吸气剂

Posted

技术标签:

【中文标题】Android Studio:房间:错误:找不到字段的吸气剂【英文标题】:Android Studio: Room: error: Cannot find getter for field 【发布时间】:2020-02-24 08:38:31 【问题描述】:

我正在使用房间数据库进行本地缓存。在我的实体中,我有其他对象,它们的 getter 具有与其用途相关的接口的返回类型。但是房间找不到字段的吸气剂。我的目标是将这些接口用作返回类型,以便我可以将来自多个远程源的数据添加到一个数组中。如何在实现灵活和良好抽象的同时做到这一点?

public interface Venue 
  Location getLocation();
  Contact getContact();


public interface Location 
  double getLongitude();
  double getLatitude();


public interface Contact 
  String getPhoneNumber();
  String getFacebookUsername();


public class VenueSourceOne implements Venue
   private VenueSourceOneLocation venueSourceOneLocation;
   private VenueSourceOneContact venueSourceOneContact;

   Location getLocation()
      return venueSourceOneLocation;
   

   Contact getContact()
      return venueSourceOneContact
   


public class VenueSourceTwo implements Venue
   private VenueSourceTwoLocation venueSourceTwoLocation;
   private VenueSourceTwoContact venueSourceTwoContact;

   Location getLocation()
      return venueSourceTwoLocation;
   

   Contact getContact()
      return venueSourceTwoContact
   



public class Main
   ....add to a List<Venue> and do something on the UI

【问题讨论】:

【参考方案1】:

出于我提出的问题以外的原因,我将场地接口更改为抽象类:

来自

public interface Venue 
  Location getLocation();
  Contact getContact();

public abstract class Venue 
      public abstract Location getLocation();
      public abstract Contact getContact();

我的问题的真正解决方案如下:

来自

public class VenueSourceOne implements Venue
   private VenueSourceOneLocation venueSourceOneLocation;
   private VenueSourceOneContact venueSourceOneContact;

   Location getLocation()
      return venueSourceOneLocation;
   

   Contact getContact()
      return venueSourceOneContact
   


public class VenueSourceTwo implements Venue
   private VenueSourceTwoLocation venueSourceTwoLocation;
   private VenueSourceTwoContact venueSourceTwoContact;

   Location getLocation()
      return venueSourceTwoLocation;
   

   Contact getContact()
      return venueSourceTwoContact
   

public class VenueSourceOne extends Venue
   private VenueSourceOneLocation location;
   private VenueSourceOneContact contact;

   @Override
   VenueSourceOneLocation getLocation()
      return location;
   

   @Override
   VenueSourceOneContact getContact()
      return contact;
   


public class VenueSourceTwo extends Venue
   private VenueSourceTwoLocation location;
   private VenueSourceTwoContact contact;

   @Override
   VenueSourceTwoLocation getLocation()
      return location;
   

   @Override
   VenueSourceTwoContact getContact()
      return contact;
   

列表在从源中检索数据时会使用抽象类指定的通用类型,而房间会使用特定类型存储数据。

【讨论】:

以上是关于Android Studio:房间:错误:找不到字段的吸气剂的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio Android 资源链接失败错误.. AAPT:错误:找不到资源 android:attr/lStar。在 values.xml 105

onClick 上的 Android Studio XML 错误“找不到相应的方法处理程序”

Android Studio R文件找不到

ionic 3 - 错误在 Android Studio 中也找不到安装的 Gradle 版本

Android studio'运行'app'时出错:找不到默认活动

Apple Silicon Android Studio 模拟器:错误:找不到模拟器可执行文件