最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

kotlin - App crash after delete item in detail screen and then navigate back to list screen - Stack Overflow

programmeradmin1浏览0评论

In DetailViewModel I have this function

fun delete(id: Int) {
  viewModelScope.launch {
    repository.delete(
       Item(
          id = id,
          name = state.name,
       )
    )
 }
}

After I click on Delete button in DetailScreen, the app crash. The delete was successful though.

Here the code in DetailScreen

FilledTonalButton(
  onClick = {
     viewModel.delete(id)
     navigateBack() //navigate to the list screen
  },               
)

The strange think is that if I put the same code in the ListViewModel works fine. I click on a item in the list, and this is deleted and the LazyColumn is updated (the deleted item there is no more)

fun delete(item: Item) {
      viewModelScope.launch {
        repository.delete(
           item
        )
     }
}

And ListScreen code

ElevatedCard (
  elevation = CardDefaults.cardElevation(
    defaultElevation = dimensionResource(id = R.dimen.dimen_8dp)
  ),
  modifier = Modifier           
      .clickable {
         listViewModel.delete(item)
      }
)

The error is

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.utils.deutsch.data.room.Item.getName()' on a null object reference

The method getName() is called into protected void bind method autogenerate by Room.

EDIT

I found the problem. In my init function in the DetailViewModel I have a Flow that tries to get Item every time that there is a modification in the database, but in case of delete item, the item is null. I modified my init function like this:

init {
        if(id!= -1){
            viewModelScope.launch {
                repository.getItem(id).collectLatest {
                    if(it != null){ //I added this check
                        state = state.copy(
                            nameIt = it.name
                        )
                    }
                }
            }
        }
    }

In DetailViewModel I have this function

fun delete(id: Int) {
  viewModelScope.launch {
    repository.delete(
       Item(
          id = id,
          name = state.name,
       )
    )
 }
}

After I click on Delete button in DetailScreen, the app crash. The delete was successful though.

Here the code in DetailScreen

FilledTonalButton(
  onClick = {
     viewModel.delete(id)
     navigateBack() //navigate to the list screen
  },               
)

The strange think is that if I put the same code in the ListViewModel works fine. I click on a item in the list, and this is deleted and the LazyColumn is updated (the deleted item there is no more)

fun delete(item: Item) {
      viewModelScope.launch {
        repository.delete(
           item
        )
     }
}

And ListScreen code

ElevatedCard (
  elevation = CardDefaults.cardElevation(
    defaultElevation = dimensionResource(id = R.dimen.dimen_8dp)
  ),
  modifier = Modifier           
      .clickable {
         listViewModel.delete(item)
      }
)

The error is

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String com.utils.deutsch.data.room.Item.getName()' on a null object reference

The method getName() is called into protected void bind method autogenerate by Room.

EDIT

I found the problem. In my init function in the DetailViewModel I have a Flow that tries to get Item every time that there is a modification in the database, but in case of delete item, the item is null. I modified my init function like this:

init {
        if(id!= -1){
            viewModelScope.launch {
                repository.getItem(id).collectLatest {
                    if(it != null){ //I added this check
                        state = state.copy(
                            nameIt = it.name
                        )
                    }
                }
            }
        }
    }
Share Improve this question edited Mar 6 at 18:41 sara asked Mar 5 at 15:42 sarasara 1843 silver badges13 bronze badges 6
  • Please edit the question and also provide the stacktrace from the crash, formatted using a code block. – tyg Commented Mar 5 at 16:56
  • @tyg I provide the error – sara Commented Mar 5 at 19:21
  • Where in your code can I find the line where the error occurs? – tyg Commented Mar 5 at 20:48
  • When there is navigateBack(). The instruction viewModel.delete(id) goes well. Going into debug I see that the transaction of delete is successful. – sara Commented Mar 6 at 17:06
  • I believe that after delete, the ScreenDetail tries to access deleted item. The navigateBack() is irrelevant. – sara Commented Mar 6 at 18:09
 |  Show 1 more comment

1 Answer 1

Reset to default 0

I found the problem. In my init function in the DetailViewModel I have a Flow that tries to get Item every time that there is a modification in the database, but in case of delete item, the item is null. I modified my init function like this:

init {
        if(id!= -1){
            viewModelScope.launch {
                repository.getItem(id).collectLatest {
                    if(it != null){ //I added this check
                        state = state.copy(
                            nameIt = it.name
                        )
                    }
                }
            }
        }
    }
发布评论

评论列表(0)

  1. 暂无评论