即使我将变量分配为 var,我也会收到“Val 无法重新签名错误”?

Posted

技术标签:

【中文标题】即使我将变量分配为 var,我也会收到“Val 无法重新签名错误”?【英文标题】:I get a "Val cannot be reasigned error" even though I assigned the variable as var? 【发布时间】:2021-09-04 13:00:02 【问题描述】:

我正在尝试向应用程序添加“复制到剪贴板”功能。我在图像按钮上添加了一个 setOnClickListener 用于复制并编写了下面的代码。我仍然在“clipBoard.primaryClip = clip”行上收到“val cannot be reassigned”错误

            var clipBoard: ClipboardManager = getActivity()?.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
            val StrEdtFirtname = etNoteDesc.text.toString()
            val clip = ClipData.newPlainText("Copied text",StrEdtFirtname)
            clipBoard.primaryClip = clip
            Toast.makeText(requireContext(), "Copied text", Toast.LENGTH_SHORT).show()
        

【问题讨论】:

【参考方案1】:

试试下面的代码。

var clipBoard: ClipboardManager = getSystemService(CLIPBOARD_SERVICE) as ClipboardManager
     val StrEdtFirtname = etNoteDesc.text.toString()
    val clip = ClipData.newPlainText("Copied text",StrEdtFirtname)
    clipBoard.apply 
        setPrimaryClip(clip)
    

【讨论】:

以上是关于即使我将变量分配为 var,我也会收到“Val 无法重新签名错误”?的主要内容,如果未能解决你的问题,请参考以下文章

为啥即使我有一个 catch() 函数,我也会收到 UnhandledPromiseRejectionWarning?

为啥即使实现了 Iterable,我也会收到 foreach 编译器错误?

为啥即使我使用 sparse_categorical_crossentrpy,我也会收到“收到超出 [0, 1) 有效范围的标签值 6”?

即使我只有一个参数,我也会收到“包装承诺不可迭代错误”

即使路径正确,shell 脚本 cd 也会失败

即使使用顺序模型,我也会收到“AttributeError:'Model' object has no attribute 'predict_classes'”