【发布时间】:2026-02-01 04:20:07
【问题描述】:
我正在尝试在适配器类中添加 loadingbar,其中相同的视图位于 mainactivity 中。我使用 doAsync 方法在单击 mainactivity 中的应用程序图标时加载应用程序.
以下是我在 doAsync 方法中使用 loadingbar 时遇到的错误。
android.view.ViewRootImpl$CalledFromWrongThreadException:只有创建视图层次结构的原始线程才能接触其视图。
下面是适配器类
Adapter.kt
class Adapter(ctx: Context, private val appInfoList: ArrayList<AppInfoModel>, private val contentResolver: ContentResolver,
private val packageManager: PackageManager,
private val loadingIndicator: AVLoadingIndicatorView,
private val applicationContext: Context) : RecyclerView.Adapter<Adapter.MyViewHolder>() {
private val inflater: LayoutInflater
init {
inflater = LayoutInflater.from(ctx)
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Adapter.MyViewHolder {
val view = inflater.inflate(R.layout.list_item, parent, false)
return MyViewHolder(view)
}
override fun onBindViewHolder(holder: Adapter.MyViewHolder, position: Int) {
var itemView: View? = null
holder.appIcon.setImageDrawable(appInfoList[position].appIcon)
holder.appName.setText(appInfoList[position].appName)
holder.versionNumber.setText(appInfoList[position].versionNumber)
if (!appInfoList[position].isInstalled) {
holder.appInfoCard.foreground = ColorDrawable(Color.parseColor("#CCFFFFFF"))
holder.appInfoCard.isEnabled = false
holder.warningIcon.visibility = View.VISIBLE
}
holder.appInfoCard.setOnClickListener() {
Log.e("App opened", appInfoList[position].appName)
var loadingBar:AVLoadingIndicatorView= loadingIndicator.findViewById(R.id.avi)
Log.d("Loading bar", loadingBar.toString())
doAsync {
try {
loadingBar!!.setVisibility(View.VISIBLE);
LauncherUtils.setDynamicConfig(contentResolver, packageManager,applicationContext, appInfoList[position].auxName!!, appInfoList[position].packageName!!)
}
catch (e: Exception) {
Log.e("Exception-launcher", e.toString())
}
uiThread {
//loadingBar!!.setVisibility(View.INVISIBLE)
}
}
}
}
任何帮助将不胜感激。
【问题讨论】:
-
您不能在 android 中触摸来自后台线程的视图。所以将
loadingBar移到 block 之外。你在使用Anko吗?
标签: android kotlin activity-indicator