首页 > 解决方案 > 谷歌语音识别开启新活动

问题描述

你好朋友我是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"

标签: javaandroidspeech-recognitiongoogle-cloud-speech

解决方案


日志错误显示为 NumberFormatException 所以,将您的代码放在 try/catch 块中并检查结果可能是应用程序不会崩溃。


推荐阅读