Google语音识别开启新activity

Google speech recognization open new activity

你好,朋友们,我是 android 的新手,在我的项目中使用 google 语音识别,如果模式匹配,我会使用正则表达式,然后转到下一个 activity,效果很好,但是我的问题是,如果模式不匹配,我的应用程序会崩溃,而不是让应用程序崩溃我想在我的应用程序中显示 toast 消息,以便用户知道如何在我的应用程序中使用语音识别,这是我的代码。

//这是我的逻辑,如果匹配模式转到下一个 activity 工作正常。

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 块中并检查结果可能是应用程序不会崩溃。