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

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个回答

2

对我而言,这是因为将AndroidX与Pre-AndroidX混合使用。在完成全面迁移并执行此操作后,一切都恢复正常了。(当然,我也转移到了AndroidX-Room)


这很有帮助! - SilverTech

2

如果有人在使用Kotlincoroutines,同时在使用Room时需要在他们的ViewModel中使用MutableLiveData<List<T>>,那么我是如何解决这个问题的:

在Dao中,我使用suspend获取MutableList
在repository中,我将上下文更改为Dispatchers.IO,并使用suspend提取列表
在ViewModel中,我使用以下语法在init中使用postValue来传递列表:

ViewModel

    private val allItems = MutableLiveData<List<DocumentItem>>()
    init {
        viewModelScope.launch {
            allItems.postValue(repository.getAll())
        }
    }

仓库

    suspend fun getAll(): MutableList<DocumentItem> = withContext(Dispatchers.IO) {
        dao.getAll()
    }

Dao

    @Query("SELECT * FROM document_items ORDER BY id DESC")
    suspend fun getAll(): MutableList<DocumentItem>

2

我有一个与我的应用程序不同的使用情况。

因此,我尝试返回实际的Cursor类型。

例如:

@Query("SELECT * FROM tbl_favourite")
abstract suspend fun selectAll(): Cursor

以上代码总是会抛出“Error:Not sure how to convert a android.database.Cursor to this method's return type”错误。
但是,据我所记,官方文档也在这里说明了Room支持Cursor
在尝试调试错误日志并打开MyTableDao_Impl.java文件后,我发现Cursorsuspend关键字之间存在不健康的关系。
因此,我已经更正了我的代码如下:
@Query("SELECT * FROM tbl_favourite")
abstract fun selectAll(): Cursor

看,它已经可以运行了。


1
在我的情况下,升级版本是有效的。
以前的。
api 'androidx.room:room-runtime:2.0.0'
annotationProcessor 'androidx.room:room-compiler:2.0.0'
api 'androidx.room:room-rxjava2:2.0.0'

现在
api 'androidx.room:room-runtime:+'
annotationProcessor 'androidx.room:room-compiler:+'
api 'androidx.room:room-rxjava2:+'

+ 意味着最新版本,对我来说是 2.4.2


1

对于我的情况,在收到“不确定如何将游标转换为此方法的返回类型”后:

删除“build”,然后重新构建,错误就会消失。


哈哈,这修复了一些毫无意义的错误!谢谢! - Tanasis

1

请确认在错误描述前您的构建日志中是否存在错误。如果有,可能是您忘记将新实体Pojo添加到数据库类中了。类似于这样的内容:

查询出现问题:[SQLITE_ERROR] SQL错误或缺失数据库(没有名为METRO的表)


请注意保留HTML标签。

@Database(entities = {Table.class,ForgottenTable.class}, version = 1) 
public abstract class Database extends RoomDatabase {
    //class codes
}

1

请确保不要将suspend与LiveData作为返回类型一起使用:

@Query("SELECT * FROM ...")
fun getAllTellsByReceiver(receiverUid: String): LiveData<List<Tell>>

0

修改你的Dao,使用Flowable而不是Observable,并添加以下依赖项(带有RxJava支持的Room)

compile group: 'android.arch.persistence.room', name: 'rxjava2', version: '1.1.1'

Dao 返回 Flowable:

@Query("SELECT * FROM TableX")
public abstract Flowable<List<EntityX>> getAllXs();

0
 class IdAndFullName {
     public int uid;
     @ColumnInfo(name = "full_name")
     public String fullName;
 }
 // DAO
 @Query("SELECT uid, name || lastName as full_name FROM user")
 public IdAndFullName[] loadFullNames();

如果查询结果与POJO不匹配,Room将会给出此错误消息。
或者,如果您正在使用@SkipQueryVerification,您也会收到此错误。

0

就我的情况而言,我从Dao中删除了我的基本Resource类,因为它不知道如何更改Resource类,这样做起作用了。

之前

fun getAll(): Flow<Resource<List<SavedCharacter>>>

之后

fun getAll(): Flow<List<SavedCharacter>>

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