单击刷新列表时 Kotlin 应用程序崩溃
我制作了一个 Android 应用程序,它使用带有适配器的自定义列表视图、刷新按钮和每行的 onlick 事件。 我的问题是,第一次列表加载一切正常,但是当我点击刷新然后单击随机行时,应用程序崩溃并出现以下错误:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
在 onlick 事件中,我有一个带有 4 个按钮的自定义对话框,错误位于最后一行(builder.create().show())。
rowView.setOnClickListener {
val builder = AlertDialog.Builder(context)
builder.setTitle(context.getString(R.string.alert_commands_desc) + " " + recipe.valve)
builder.setItems(
arrayOf<CharSequence>(context.getString(R.string.open_valve_button),
context.getString(R.string.close_valve_button),
context.getString(R.string.deny_command_button),
context.getString(R.string.close))
) { dialog, which ->
when (which) {
0 -> sendValveCommand("AV", recipe.id, context)
1 -> sendValveCommand("CV", recipe.id, context)
2 -> sendValveCommand("AC", recipe.id, context)
3 -> dialog.dismiss()
}
}
builder.create().show()
}
这是适配器部分:
if (json.has("Apparati_Controllo")) {
val controlElementList = json.getJSONArray("Apparati_Controllo")
val recipeList = ControlElement.populateRecipe(controlElementList)
val adapter = ControlElementAdapter(thisContext, recipeList)
listView.adapter = adapter
}
我尝试清除适配器、列表、更改上下文,但没有任何效果... 非常感谢,请告诉我您是否需要更多代码。
I've made an Android app that use a custom listview with an adapter, a refresh button and an onlick event for each row.
My problem is that the first time that the list load all works well, but when i hit refresh then i click on a random row the app crashes with the following error:
android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
In the onlick event i have a custom dialog with 4 buttons, the error is in the last row (builder.create().show()).
rowView.setOnClickListener {
val builder = AlertDialog.Builder(context)
builder.setTitle(context.getString(R.string.alert_commands_desc) + " " + recipe.valve)
builder.setItems(
arrayOf<CharSequence>(context.getString(R.string.open_valve_button),
context.getString(R.string.close_valve_button),
context.getString(R.string.deny_command_button),
context.getString(R.string.close))
) { dialog, which ->
when (which) {
0 -> sendValveCommand("AV", recipe.id, context)
1 -> sendValveCommand("CV", recipe.id, context)
2 -> sendValveCommand("AC", recipe.id, context)
3 -> dialog.dismiss()
}
}
builder.create().show()
}
This is the adapter part:
if (json.has("Apparati_Controllo")) {
val controlElementList = json.getJSONArray("Apparati_Controllo")
val recipeList = ControlElement.populateRecipe(controlElementList)
val adapter = ControlElementAdapter(thisContext, recipeList)
listView.adapter = adapter
}
I tried by clearing adapter, list, change context, nothing works...
Thanks a lot, tell me if you need more code.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我从来不喜欢对任何需要显示的视图使用
this
或getApllicationContext()
。而是使用这个:同时将其添加到您的
AndroidManifest.xml
中I never prefer using
this
orgetApllicationContext()
for any view that needs to be shown. Instead use this:Also add this in your
AndroidManifest.xml