方法是哪个?“不确定如何将游标转换为此方法的返回类型”的房间:

116
Error:Not sure how to convert a Cursor to this method's return type
Error:Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.

使用 Room 时,我遇到了这个错误,我想找出是哪个方法导致的。

我有多个 DAO,总共大约有60个方法,在添加一个方法后出现了这个错误(复制并粘贴自另一个完美工作的方法,只更改了字段)。

我可以发布整个 DAO 类,但是我要求知道失败的方法。 我尝试过使用 Run with --stacktraceRun with --info--debug option,但这些都没有显示任何有价值的信息。

我添加的方法是一个具有 @Query UPDATEInt 返回类型的方法,如文档所建议。

UPDATE 或 DELETE 查询可以返回 void 或 int。 如果它是 int,则该值是此查询影响的行数。

编辑:我想补充说明,我尝试删除该方法,将 DAO 恢复到正常状态,但仍会出现此错误。

编辑2:添加 Gradle 控制台输出,因为在注释中无法阅读:

error: Not sure how to convert a Cursor to this method's return type
error: Not sure how to convert a Cursor to this method's return type
2 errors

:app:compileDebugJavaWithJavac FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
Compilation failed; see the compiler error output for details.

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

* Get more help at https://help.gradle.org

BUILD FAILED in 22s

1
Gradle控制台中的完整错误消息是什么? - pskink
@pskink将完整的错误信息添加到了问题中。 - David Corsalini
1
这是我的方法,它可以编译通过:@Query("SELECT * FROM user") LiveData<List<User>> loadUsers(); 当我将 List 更改为 Vector 时,例如:LiveData<Vector<User>> loadUsers(); 我会得到以下错误信息:在 Android Studio 的 Gradle Console 窗口中显示了行位置 UserDao.java:19,并打印出了错误的方法,错误信息为 error: Not sure how to convert a Cursor to this method's return type。更多信息请参见 https://developer.android.com/studio/run/index.html#gradle-console。 - pskink
1
我正在使用 Kotlin 进行构建,也许这就是为什么我不理解这一行的原因? - David Corsalini
5
今天我遇到了相同的问题(返回类型是rxjava2 Observable)。错误消息中没有任何关于问题所在的信息。我希望Google能够使错误消息更加详细。至少应该打印出方法名称或期望的返回类型... - ntoskrnl
显示剩余6条评论
32个回答

0
另一个可能导致这种情况发生的原因是,如果您尝试使用ArrayList而不是List,它将无法识别返回类型。
例如使用:
@Query("SELECT * FROM notificationTable ORDER BY id ASC")
fun getAllNotifications(): LiveData<List<UserNotification>>

替代

@Query("SELECT * FROM notificationTable ORDER BY id ASC")
fun getAllNotifications(): LiveData<ArrayList<UserNotification>>

0

如果有人遇到这个问题,只需更新您的依赖项(room)
这对我有用:D


目前你的回答不够清晰。请编辑并添加更多细节,以帮助其他人理解它如何回答所提出的问题。你可以在帮助中心找到有关如何撰写好答案的更多信息。 - Community
Room版本2.4.32022年7月27日漏洞修复修复了一个问题,该问题会导致Room无法识别Kotlin 1.7中的挂起函数(b/236612358)。 - Maxdestroyer

0

你必须在方法返回的类中包含@Relation注释。这是Room知道如何建立两者之间关系的唯一方法。


0
对我来说,我在查询中使用了错误的返回类型。

0

将以下代码添加到build.gradle文件的defaultConfig中

javaCompileOptions.annotationProcessorOptions.includeCompileClasspath = true

3
为什么? - jesses.co.tt
2
我们希望对此进行解释,因为这个解决方案没有起作用。 - Ankit Gupta

0
经过一些搜索,发现您需要将 Room 版本升级到 2.3.0-alpha02 或更高版本:
implementation "androidx.room:room-runtime:2.3.0-alpha02"
implementation "androidx.room:room-ktx:2.3.0-alpha02"
kapt "androidx.room:room-compiler:2.3.0-alpha02"

现在它运行良好


0

我通过升级我的房间版本来解决了这个问题

implementation "androidx.room:room-runtime:$roomVersion"
kapt "androidx.room:room-compiler:$roomVersion"
implementation "androidx.room:room-ktx:$roomVersion"

将房间版本从roomVersion = '2.3.0'升级到roomVersion = '2.5.2'


0
当我在实现分页3库时,我遇到了同样的问题。 我通过将我的Room依赖版本更新到最新的稳定版本,并添加room-paging支持来解决它。
implementation "androidx.room:room-paging:$room_version"

-1
当我尝试在查询中执行一些聚合函数,如sum和count,并在列名中使用别名时,出现了以下错误: select count(users.id) as userCount, ... 事实证明,像上面的userCount这样的别名名称必须匹配模型中的字段名称。

-1
在我的情况下,Room 不知道如何将 Cursor 转换为此方法的返回类型,即 ArrayList。因此,我稍微改了一下,将其转换为 Kotlin 的 MutableListOf 列表。现在它可以正常工作了。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接