Android Room 不接受通用字段类型

标签 android android-room

我试图拥有一个基类,其 id 字段属于通用类型,例如

class BaseEntity<T>{
  private T id;
  //get, set;
}
class User extends BaseEntity<String> {
  //blah blah blah
}

我有一个像这样的 DAO:

@Dao
public interface UserDao {

    @Query("SELECT * FROM user WHERE id = :id")
    Flowable<User> getUserById(String id);
}

我收到编译错误:

app\build\generated\source\apt\irrisimples\debug\com\irrisimples\data\source\local\UserDao_Impl.java:275: error: cannot find symbol final T _tmpId; ^

在 UserDao_Impl:275 中:

final T _tmpId; _tmpId = _cursor.getString(_cursorIndexOfId);

所以这意味着 Room 无法弄清楚 T 是什么并替换那里的值。这是一个错误还是我做错了什么?

P.S:使用 Android Studio 3.1 BETA 3 和 Room 1.0.0(已测试 1.1.0-alpha2)

最佳答案

引用Google Issue Tracker

应该在 1.1.0-alpha3 中修复

关于Android Room 不接受通用字段类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48845327/

相关文章:

android - 有没有办法在 picasso 中加载默认图像

android - 从 Android USB 附件中读取数据

android - Droid Razr - 它是高、中或低 dpi 设备吗?

java - 如何将Repository自动生成的ID返回到MainActivity?

android - 如何创建与 Room 一起使用的 sqlite View ?

android - 错误 : Cannot find setter for field. - java.util.ArrayList 中的大小 - Room 中的嵌入式 ArrayList 无法编译

java - Arraylist.get 返回 null

android - 不允许加载本地资源 : ionic 3 android

android - 未使用房间的项目中房间的运行时异常

即使数据库不为空,Android Room Select 查询也会返回 null 或空字符串