简体   繁体   English

如何在 AsyncTask 中使用 findViewById 方法?

[英]How can I use findViewById method inside AsyncTask?

I have an AsyncTask to execute while trying to connect the phone to a Bluetooth device, I want to change an icon from a button if it connects successfully我在尝试将手机连接到蓝牙设备时执行 AsyncTask,如果连接成功,我想从按钮更改图标

(I'm new to Kotlin programming) (我是 Kotlin 编程新手)

主要代码

在此处输入图像描述

I would be very grateful if anyone could help.如果有人能提供帮助,我将不胜感激。

将此替换为第 146 行 val blueBtn=this@MainActivity.findViewById(R.id.blue_btn)

I would like to mention, that making changes from the async task is not really a best solution, since they are designed to handle only background jobs, so I would recommend sending a command from there to handle in the Activity我想提一下,从异步任务中进行更改并不是真正的最佳解决方案,因为它们旨在仅处理后台作业,因此我建议从那里发送命令以在 Activity 中处理

Still, if you want this, try changing you passed parameter context to be of type Activity , like context: Activity , and access findViewById by context.findViewById(...)尽管如此,如果你想要这个,尝试将你传递的参数context更改为Activity类型,如context: Activity ,并通过context.findViewById(...)访问findViewById

Replace private to inner class.私有替换为内部class。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM