Val can not be reassigned at ClipboardManager prim

2020-08-01 06:01发布

I know this question is already been asked but still, it does not help me to resolve issue

I am getting issue while copy data to clipboard-manager

below is code

fun copyToClipboard(context: Context, text: CharSequence){
        var clipboard: ClipboardManager = context.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
        var clip: ClipData = ClipData.newPlainText("label",text)
        clipboard.primaryClip = clip!!
    }

as @ianhanniballake suggested, I have already use use setPrimaryClip() with a non-null ClipData

like below

var clip: ClipData = ClipData.newPlainText("label",text)
clipboard.primaryClip = clip!!

But don't know why, still I am getting same error like

Val cannot be reassigned

1条回答
甜甜的少女心
2楼-- · 2020-08-01 06:04

You must use method setPrimaryClip(), for example:

clipboard?.setPrimaryClip(clip)

because field primaryClip has only getter.

查看更多
登录 后发表回答