Room “Not sure how to convert a Cursor to this method's return type”: which method?

后端 未结 20 1375
遇见更好的自我
遇见更好的自我 2020-12-14 14:15
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          


        
相关标签:
20条回答
  • 2020-12-14 15:10

    For me it was to change from MutableLiveData to LiveData as the return type of the get method.

    0 讨论(0)
  • 2020-12-14 15:11

    In my case I was using androidx dependencies for Room and android.arch. [old] dependencies for ViewModel and LiveData so I got this message

    Solution: Either use all androidx dependencies OR use all old dependencies of andrio.arch

    0 讨论(0)
  • 2020-12-14 15:12

    Yup, based on what you have mentioned in the comments, you are not allowed to change the return type from List to anything else inside the Dao. I'd assume Room doesn't know how to deal with other return types. Take the List and cast/convert it into your desired type outside of the Dao.

    0 讨论(0)
  • 2020-12-14 15:12

    For me it was because of mixing AndroidX with Pre-AndroidX. After a full migration and performing this, everything was back to normal. (Of course I moved to AndroidX-Room as well)

    0 讨论(0)
  • 2020-12-14 15:12

    You have to include the @Relation annotation in the class returned by the method. It's the only way Room would know how to establish the relationship between the two.

    0 讨论(0)
  • 2020-12-14 15:15
     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();
    

    If there is a mismatch between the query result and the POJO, Room will give you this error message.

    Or if you are using @SkipQueryVerification, you will also get this error.

    0 讨论(0)
提交回复
热议问题