java - 谷歌语音识别开启新活动
问题描述
你好朋友我是android的新手,在我的项目中使用谷歌语音识别,如果模式匹配,我使用正则表达式进入下一个完美运行的活动,但我的问题是,如果模式不匹配,我的应用程序崩溃而不是崩溃我想在我的应用程序中显示 toast 消息,以便用户知道如何在我的应用程序中使用语音识别这是我的代码。
//如果模式匹配,这是我的逻辑转到下一个工作正常的活动。
if(useList(array,bookname)&&chapter.contains("chapter")&&chapternumber.matches("[1-9]+")&&verse.contains("verse")
&&versenumber.matches("[1-9]+")){
Intent intent = new Intent(MainActivity.this, ALLVERSE.class);
intent.putExtra("Boooknumber", booknumber);
intent.putExtra("Bookname", bookname);
intent.putExtra("Chapternumber", chapternumberindex);
intent.putExtra("versenumber", vereseno);
startActivity(intent);
Toast.makeText(this, "MACTHED", Toast.LENGTH_SHORT).show();
}
//my else part if not matched instead showing false in toast my app is crashed
else{
Toast.makeText(this, "false", Toast.LENGTH_SHORT).show();
}
这是日志错误:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=10, result=-1, data=Intent { (has extras) }} to activity {bible.swordof.God/bible.swordof.God.MainActivity}: java.lang.NumberFormatException: Invalid int: "am"
at android.app.ActivityThread.deliverResults(ActivityThread.java:3714)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3757)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1394)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5452)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:762)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:652)
Caused by: java.lang.NumberFormatException: Invalid int: "am"
解决方案
日志错误显示为 NumberFormatException 所以,将您的代码放在 try/catch 块中并检查结果可能是应用程序不会崩溃。
推荐阅读
- python - 可以在 Enum 中添加自定义错误以显示有效值吗?
- python - 带有 UniqueConstraint 的 Django IntegrityError
- windows - 如何从 Windows 命令行或批处理确定插入的空白可刻录光介质(CD、DVD、蓝光光盘)的容量?
- cmake - 如何让 CMake 制作自定义脚本?
- visual-studio-code - "__" 未访问 | 皮兰斯 | 币安API | VSCode
- awk - 在 bash 中使用十进制整数和浮点数打印
- excel - 根据前一天输入的内容清除单元格范围
- kubernetes - 在 Open Telemetry Collector 中使用 OTEL_EXPORTER_OTLP_HEADERS 来避免 ConfigMap 中的秘密
- python - 引用相同的数据框时,数据框的大小不同?
- angular - 以角度测试表单生成器时出错