当我在Android Studio中使用数据绑定时,无法智能地将错误强制转换为“ HomeViewModel”吗?

问题描述

我使用HomeViewModel<variable name="aHomeViewModel"type="info.dodata.voicerecorder.viewcontrol.HomeViewModel" />绑定到layout.xml

运行代码时出现以下错误。

不可能将类型强制转换为“ HomeViewModel”,因为“ binding.aHomeViewModel”是可变的属性,可能此时已经更改了

我认为binding.aHomeViewModel.delete(binding.amVoice)导致了错误,该如何解决?

VoiceAdapters.kt

class VoiceAdapters (private val aHomeViewModel: HomeViewModel):
        ListAdapter<MVoice,VoiceAdapters.VoiceViewHolder>(MVoiceDiffCallback()) {

    override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): VoiceViewHolder {
        return VoiceViewHolder(
            LayoutVoiceItemBinding.inflate(LayoutInflater.from(parent.context),parent,false)
        )
    }

    override fun onBindViewHolder(holder: VoiceViewHolder,position: Int) {
        val aMVoice = getItem(position)
        holder.bind(aHomeViewModel,aMVoice)
    }

    class VoiceViewHolder (private val binding: LayoutVoiceItemBinding):
          RecyclerView.ViewHolder(binding.root) {

        init {
            setControl()
        }

        private fun navigateToDetail( view: View,aMVoice: MVoice )
        {
            val direction = FragmentHomeDirections.actionHome2Detail(aMVoice)
            view.findNavController().navigate(direction)
        }

        fun bind(aHomeViewModel: HomeViewModel,aMVoice: MVoice){
            binding.amVoice=aMVoice
            binding.aHomeViewModel=aHomeViewModel
            binding.executePendingBindings()
        }

        fun setControl(){              
            binding.btnDelete.setOnClickListener {
                binding.amVoice?.let {
                  binding.aHomeViewModel?.let {
                     binding.aHomeViewModel.delete(binding.amVoice)  //I think it cause error.
                  }
              }
            }
        }
    }

}


class MVoiceDiffCallback : DiffUtil.ItemCallback<MVoice>() {
   ...
}

HomeViewModel.kt

class HomeViewModel(private val mDBVoiceRepository: DBVoiceRepository) : ViewModel() {
    ...
    
    fun delete(aMVoice: MVoice){
        viewModelScope.launch {
            mDBVoiceRepository.deleteVoice(aMVoice)
        }
    }

}

layout.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <variable name="aMVoice"
        type="info.dodata.voicerecorder.model.MVoice"  />

    <variable name="aHomeViewModel"
        type="info.dodata.voicerecorder.viewcontrol.HomeViewModel" />
</data>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:orientation="horizontal">

    <TextView
       android:id="@+id/voiceID"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@{Integer.toString(aMVoice.id)}" />

    <TextView
        android:id="@+id/title_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50dp"
        android:text="@{aMVoice.name}" />
    ...
</LinearLayout>

</layout>

添加的内容

如果使用代码A,我将获得新的编译警告信息Warning A。

代码A

binding.btnDelete.setOnClickListener {
    binding.aHomeViewModel!!.delete(binding.amVoice!!)
}

警告A

在此版本中使用了不推荐使用的Gradle功能,使其与Gradle 7.0不兼容。 使用“ --warning-mode all”来显示各个弃用警告。

解决方法

HomeViewModel是一个可变的属性,这一次可能已更改。必须与!!?接线员一起使用安全通话

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...