Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 重载的dispatchKeyEvent方法在应用程序中添加一些后期处理代码时会冻结应用程序_Android_Kotlin_Barcode Scanner_Keyevent - Fatal编程技术网

Android 重载的dispatchKeyEvent方法在应用程序中添加一些后期处理代码时会冻结应用程序

Android 重载的dispatchKeyEvent方法在应用程序中添加一些后期处理代码时会冻结应用程序,android,kotlin,barcode-scanner,keyevent,Android,Kotlin,Barcode Scanner,Keyevent,我目前正在开发一个应用程序,它使用条形码扫描仪扫描一些条形码。因此,我通过重载dispatchKeyEvent方法来捕获数据。只要我不想对提示的条形码进行后期处理,一切正常 例如,此代码段按预期工作 private-var-prompteden:String=“” 覆盖fun dispatchKeyEvent(事件:KeyEvent?):布尔值{ if(event?.action==KeyEvent.action\u DOWN){ val pressedKey:Char=event.getUni

我目前正在开发一个应用程序,它使用条形码扫描仪扫描一些条形码。因此,我通过重载dispatchKeyEvent方法来捕获数据。只要我不想对提示的条形码进行后期处理,一切正常

例如,此代码段按预期工作

private-var-prompteden:String=“”
覆盖fun dispatchKeyEvent(事件:KeyEvent?):布尔值{
if(event?.action==KeyEvent.action\u DOWN){
val pressedKey:Char=event.getUnicodeChar().toChar()
Prompteden+=按按键
val eTMengeKomm=findviewbyd(R.id.eTMengeKomm)
eTMengeKomm.setText(prompteden,TextView.BufferType.EDITABLE)
val etCharge=findViewById(R.id.etCharge)
etCharge.setText(Prompteden,TextView.BufferType.EDITABLE)
val etMHD=findviewbyd(R.id.etMHD)
setText(prompteden,TextView.BufferType.EDITABLE)
}
返回super.dispatchKeyEvent(事件)
}
但是,只要我想在dispatchKeyEvent方法中对提示的条形码进行后期处理,整个应用程序就会冻结。在这种情况下,我必须转到设置->应用->强制停止以终止我的应用,因为所有按钮都不起作用

我的后处理代码没有任何问题——我在活动的onCreate方法中使用测试字符串进行了测试

我的问题是为什么我用dispatchKeyEvent方法得到这种行为

这是我的密码。我用OnCreate方法中的键值对填充HashMap,该方法在触发dispatchKeyEvent之前启动

private-var-prompteden:String=“”
私人var TestPrompteden:String=“”/“C10207613037927200015200600370098$1092547017L4”
private val hashMap:hashMap=hashMap()
覆盖fun dispatchKeyEvent(事件:KeyEvent?):布尔值{
if(event?.action==KeyEvent.action\u DOWN){
val pressedKey:Char=event.getUnicodeChar().toChar()
Prompteden+=按按键
testprompteden=prompteden
val eTMengeKomm=findviewbyd(R.id.eTMengeKomm)
eTMengeKomm.setText(prompteden,TextView.BufferType.EDITABLE)
val etCharge=findViewById(R.id.etCharge)
etCharge.setText(Prompteden,TextView.BufferType.EDITABLE)
val etMHD=findviewbyd(R.id.etMHD)
setText(prompteden,TextView.BufferType.EDITABLE)
如果(testprompteden.contains(“C1”,true)或testprompteden.contains(“C0”,true)){//EAN-128代码
变量位置:Int=2//init位置
var tail:String=testprompteden.substring(pos)//init tail
var lookInString:String=“”
而(pos=testprompteden.length-1){tail=”“}
else{tail=testprompteden.substring(pos)}
if(searchString.contains(“37”,false)){
//val eTMengeKomm=findviewbyd(R.id.eTMengeKomm)
eTMengeKomm.setText(lookInString,TextView.BufferType.EDITABLE)
}
if(searchString.contains(“10”,false)){
//val etCharge=findViewById(R.id.etCharge)
etCharge.setText(lookInString,TextView.BufferType.EDITABLE)
}
if(searchString.contains(“15”,false)){
//val etMHD=findviewbyd(R.id.etMHD)
setText(lookInString,TextView.BufferType.EDITABLE)
}
}
}
}
}
返回super.dispatchKeyEvent(事件)
}

非常感谢您的帮助。

我是如何解决这个问题的:

首先,我去掉了重载的
dispachKeyEvent
方法。这在我的应用程序中造成了很大的混乱,在我的例子中,背部和中间的按钮根本没有反应。在安卓系统中,条形码USB扫描器主要应被识别为外部键盘,因此将条形码捕获到特定的
EditText
字段中时不需要此方法。之后,我将焦点设置在第一个
EditText
字段上,并在那里提示条形码

其次,我了解到
while
循环在活动UI线程中不起作用——这就是为什么我的应用程序一直处于冻结状态的原因。这就是为什么我在活动中将
AsyncTask
实现为内部类,并将所有回溯处理逻辑从
AsyncTask
放置到重载的
doInBackround
方法中。在
onPostExecute
方法中,我已经为我的
EditText
字段设置了所有需要的值


更新:我将整个后台逻辑提升到一个
ViewModel
中,并最终摆脱了
AsyncTask

可能由于某种原因,循环从未存在过。你有没有用调试器一步一步地检查代码?如果不是,我强烈推荐。为什么要在dispatchKeyEvent中这样做?在我看来,您只想处理一次条形码,而不是在输入的每个字母上处理一次。@Enselic循环存在,我正在使用调试器测试代码。正如我在文章中提到的,如果代码在dispatchkeyevent方法之外运行,那么它可以正常工作。如果使用
private var testprompteden:String=“C10207613037927270015200600370098$1092547017L4
,只需添加散列键值类型separately@dharms这是唯一一个对我的条形码扫描仪有效的事件,另一方面,我找不到任何其他方法可以立即处理条形码,这意味着此方法会被多次调用,直到
ENTER
代码到达末尾