Search code examples
javaandroidspeech-recognitiongoogle-cloud-speech

Google speech recognization open new activity


Hello friends I am new in android using google speech recognition in my project and I used the regex expression if the pattern matches it go to next activity which works perfectly but my problem is that if pattern is not matched my app is crashed instead of crashing the app I want show toast message in my app so user know how to use speech recognition in my app here is my code.

//here is my logic if pattern is matched go to next activity which work fine.

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();
               } 

here is the log error:

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"

Solution

  • the log error is showing like NumberFormatException so, put your code in try/catch block and check the result may be app will not crash.