kotlin LiveData崩溃:转换为Map崩溃

wko9yo5t  于 2023-06-24  发布在  Kotlin
关注(0)|答案(2)|浏览(166)

我有以下代码:

private var bookmarkDetailsView: LiveData<BookmarkDetailsView>? = null

private fun mapBookmarkToBookmarkView(bookmarkId: Long) {
    val bookmark = bookmarkRepo.getLiveBookmark(bookmarkId)
    bookmarkDetailsView = Transformations.map(bookmark) { repoBookmark ->
    repoBookmark?.let { repoBookmark ->
          bookmarkToBookmarkView(repoBookmark)
    }
  }
}

因为Transformationdeprecated,所以我使用Map

private fun mapBookmarkToBookmarkView(bookmarkId: Long) {
    val bookmark = bookmarkRepo.getLiveBookmark(bookmarkId)
     bookmarkDetailsView = bookmark.map { repoBookmark ->
         bookmarkToBookmarkView(repoBookmark)
    }
}

然而,一旦书签从数据库中删除,我就会出现以下崩溃:

E FATAL EXCEPTION: main 
Process: com.raywenderlich.placebook, PID: 6512 
java.lang.NullPointerException: Parameter specified as non-null is null: method com.raywenderlich.placebook.viewmodel.BookmarkDetailsViewModel$mapBookmarkToBookmarkView$1.invoke, parameter kausi_repoBookmark at com.raywenderlich.placebook.viewmodel.BookmarkDetailsViewModel$mapBookmarkToBookmarkView$1.invoke(Unknown Source:2)
at com.raywenderlich.placebook.viewmodel.BookmarkDetailsViewModel$mapBookmarkToBookmarkView$1.invoke(BookmarkDetailsViewModel.kt:50)
at androidx.lifecycle.Transformations$map$1.invoke(Transformations.kt:52) 
at androidx.lifecycle.Transformations$map$1.invoke(Transformations.kt:52)  at androidx.lifecycle.Transformations$sam$androidx_lifecycle_Observer$0.onChanged(Unknown Source:2)
at androidx.lifecycle.MediatorLiveData$Source.onChanged(MediatorLiveData.java:171)
at androidx.lifecycle.LiveData.considerNotify(LiveData.java:133)  
at androidx.lifecycle.LiveData.dispatchingValue(LiveData.java:151)  
at androidx.lifecycle.LiveData.setValue(LiveData.java:309)  
at androidx.lifecycle.LiveData$1.run(LiveData.java:93)  
at android.os.Handler.handleCallback(Handler.java:958)  
at android.os.Handler.dispatchMessage(Handler.java:99)  
at android.os.Looper.loopOnce(Looper.java:205)  
at android.os.Looper.loop(Looper.java:294)  
at android.app.ActivityThread.main(ActivityThread.java:8128)  
at java.lang.reflect.Method.invoke(Native Method)  
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:578)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:946

我做错了什么?如果你能解释一下就好了。

更新

fun getLiveBookmark(bookmarkId: Long): LiveData<Bookmark> = bookmarkDao.loadLiveBookmark(bookmarkId)

@Query("SELECT * FROM Bookmark WHERE id =:bookmarkId")
fun loadLiveBookmark(bookmarkId: Long): LiveData<Bookmark>

更新2

我做了以下更改:

fun getLiveBookmark(bookmarkId: Long): LiveData<Bookmark?> = bookmarkDao.loadLiveBookmark(bookmarkId)

现在我得到了另一个错误:

private fun mapBookmarkToBookmarkView(bookmarkId: Long) {
    val bookmark = bookmarkRepo.getLiveBookmark(bookmarkId)
    if (bookmark != null) {
        bookmarkDetailsView = bookmark.map { repoBookmark ->
            repoBookmark?.let { // error is on this line
                bookmarkToBookmarkView(it)
            }
       }
    }
}

错误:

Type mismatch: inferred type is BookmarkDetailsViewModel.BookmarkDetailsView? but BookmarkDetailsViewModel.BookmarkDetailsView was expected
f45qwnt8

f45qwnt81#

您已经将LiveData对象声明为LiveData<Bookmark>,这意味着map和其他转换假定它们只接收Bookmark-例如,非空对象。在使用Transformations.map时,这不是一个“问题”,因为Java不关心空性,但Kotlin关心。
因此,如果你想让你的仓库返回一个可以返回空书签的LiveData,那么你需要将仓库返回的类型更改为使用LiveData<Bookmark?>-一个可以为空的类型。
这同样适用于您的LiveData<BookmarkDetailsView>--如果您也想支持可空条目,那么它可能也应该是LiveData<BookmarkDetailsView?>

vlju58qv

vlju58qv2#

使用下面的命令生成计算结果non-null,因为将let?一起使用会推断某些元素可能是null

bookmark.mapNotNull { repoBookmark -> bookmarkToBookmarkView(repoBookmark) }

相关问题