Android 如何将加载条添加到适配器类中,其中视图位于另一个活动中
我试图在适配器类中添加Android 如何将加载条添加到适配器类中,其中视图位于另一个活动中,android,kotlin,activity-indicator,Android,Kotlin,Activity Indicator,我试图在适配器类中添加loadingbar,该类的视图位于mainactivity中。我在单击mainactivity中的应用图标时使用了doAsync方法加载应用。 下面是我在doAsync方法中使用loadingbar时遇到的错误 android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图 下面是适配器类 Adapter.kt class Adapter(ctx: Context, priva
loadingbar
,该类的视图位于mainactivity中。我在单击mainactivity中的应用图标时使用了doAsync
方法加载应用。
下面是我在doAsync
方法中使用loadingbar时遇到的错误
android.view.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图
下面是适配器类
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)
}
}
}
}
类适配器(ctx:Context,private-val-appInfoList:ArrayList,private-val-contentResolver:contentResolver,
专用val packageManager:packageManager,
专用val加载指示器:AVLoadingIndicator视图,
private val applicationContext:Context):RecyclerView.Adapter(){
私人val充气机:充气机
初始化{
充气机=充气机从(ctx)
}
重写CreateViewHolder(父级:ViewGroup,viewType:Int):Adapter.MyViewHolder{
val视图=充气机。充气(R.layout.list_项,父项,false)
返回MyViewHolder(视图)
}
覆盖onBindViewHolder(holder:Adapter.MyViewHolder,位置:Int){
var itemView:视图?=null
holder.appIcon.setImageDrawable(appInfoList[position].appIcon)
holder.appName.setText(appInfoList[position].appName)
holder.versionNumber.setText(appInfoList[position].versionNumber)
如果(!appInfoList[position].isInstalled){
holder.appInfoCard.foreground=ColorDrawable(Color.parseColor(“#CCFFFFFF”))
holder.appInfoCard.isEnabled=false
holder.warningIcon.visibility=View.VISIBLE
}
holder.appInfoCard.setOnClickListener(){
Log.e(“应用程序已打开”,appInfoList[position].appName)
var loadingBar:avLoadingIndicator视图=loadingIndicator.findViewById(R.id.avi)
Log.d(“加载条”,加载条.toString())
doAsync{
试一试{
加载条!!.setVisibility(View.VISIBLE);
Launchutils.setDynamicConfig(contentResolver、packageManager、applicationContext、appInfoList[position].auxName!!、appInfoList[position].packageName!!)
}
捕获(e:例外){
Log.e(“异常启动器”,例如toString())
}
超线程{
//加载栏!!.setVisibility(视图.不可见)
}
}
}
}
任何帮助都将不胜感激。正如stacktrace所说,您不应该从后台线程设置任何视图的可见性。可能需要将
loadingBar!!.setVisibility(view.VISIBLE);
移动到uiThread{}
将解决您的问题。在android中,您不能从后台线程触摸视图。因此,请将加载栏移动到块外的部分。您是否正在使用Anko?