2016-08-08 9 views
1

Kurzbeschreibung meiner Anwendung:Wie Text von Speech To Text API zu Text in einer ArrayList/String Array zu vergleichen? so weit

Der Benutzer ein Wort wird das Wort in ein Arraylist Ergebnis gespeichert wird sagen wird, dann wird es auf die Worte in der String-Array vergleichen, die in der Datei gespeichert ist wordBank.xml . Wenn das Wort mit einem Wort in der Wortdatenbank übereinstimmt, wird in einer Textansicht das Wort "Übereinstimmung" angezeigt, und wenn nicht, wird "Keine Übereinstimmung" angezeigt.

Problem: Das Problem ist der Vergleich der beiden Arraylist, innerhalb meiner OnActivityResult -Funktion. Es ist ein Laufzeitfehler, dieser Bildschirm stürzt ab, wenn ich versuche, es zu starten, aber wenn ich den Code ändere, so dass er nicht vergleicht, wird er ohne einen Fehler laufen.

Main.Java

public class Main extends Activity { 

    private static final int VR_Request = 100; 

    TextView speechInput; 
    TextView matchOrNot; 
    ArrayList<String> wordBank; 
    ImageButton speechBtn; 

    boolean word = false; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_reverse_pictionary); 

     speechInput = (TextView) findViewById(R.id.english_word); 
     matchOrNot = (TextView) findViewById(R.id.matchOrNot); 
     wordBank = new ArrayList<>(R.array.Words); 
     speechBtn = (ImageButton) findViewById(R.id.mic_pic_button); 

    } 

    public void onMic(View view) { 
     if (view.getId() == R.id.mic_pic_button) { 
      promptSpeechInput(); 
     } 
    } 

    public void promptSpeechInput() { 
     Intent intent = new Intent(RecognizerIntent.ACTION_RECOGNIZE_SPEECH); 
     intent.putExtra(RecognizerIntent.ACTION_RECOGNIZE_SPEECH, RecognizerIntent.LANGUAGE_MODEL_FREE_FORM); 
     intent.putExtra(RecognizerIntent.EXTRA_LANGUAGE, Locale.getDefault()); 
     intent.putExtra(RecognizerIntent.EXTRA_PROMPT, "Say a Word from our word bank!"); 

     try { 
      startActivityForResult(intent, VR_Request); 
     } catch (ActivityNotFoundException a) { 
      Toast.makeText(ReversePictionary.this, "Oopa, your device doesn't support speech recognition,", Toast.LENGTH_LONG).show(); 
     } 
    } 

    public void onActivityResult(int requestCode, int resultCode, Intent intent) { 

     if(requestCode == VR_Request && resultCode == RESULT_OK) { 
      ArrayList<String> result = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

      if(wordBank.contains(result.get(0))) { 
       speechInput.setText(result.get(0).toUpperCase()); 
       matchOrNot.setText("MATCH"); 
      }else { 
       speechInput.setText(result.get(0)); 
       matchOrNot.setText("NO MATCH"); 
      } 
     } 
     super.onActivityResult(requestCode, resultCode, intent); 
    } 
} 

wordBank.xml

<resources> 
    <string-array name="Words"> 
     <item>Blue</item> 
     <item>Yellow</item> 
     <item>Green</item> 
     <item>Purple</item> 
     <item>Brown</item> 
     <item>White</item> 
     <item>Black</item> 
     <item>Pink</item> 
    </string-array> 
</resources> 

onActivityResult Funktion

public void onActivityResult(int requestCode, int resultCode, Intent intent) { 

    if(requestCode == VR_Request && resultCode == RESULT_OK) { 
     ArrayList<String> result = intent.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS); 

     if(wordBank.contains(result.get(0))) { 
      speechInput.setText(result.get(0).toUpperCase()); 
      matchOrNot.setText("MATCH"); 
     }else { 
      speechInput.setText(result.get(0)); 
      matchOrNot.setText("NO MATCH"); 
     } 
    } 
    super.onActivityResult(requestCode, resultCode, intent); 
} 

EDIT: Hier ist die Fehlermeldung:

08-08 01:30:31.458 2428-2428/com.example.speechtotext E/AndroidRuntime: FATAL EXCEPTION: main 
                      Process: com.example.speechtotext, PID: 2428 
                      java.lang.OutOfMemoryError: java.lang.Object[] of length 2131427328 would overflow 
                       at java.util.ArrayList.<init>(ArrayList.java:75) 
                       at com.example.speechtotext.Main.onCreate(ReversePictionary.java:43) 
                       at android.app.Activity.performCreate(Activity.java:6237) 
                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) 
                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) 
                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
                       at android.app.ActivityThread.-wrap11(ActivityThread.java) 
                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                       at android.os.Looper.loop(Looper.java:148) 
                       at android.app.ActivityThread.main(ActivityThread.java:5417) 
                       at java.lang.reflect.Method.invoke(Native Method) 
                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
08-08 01:30:35.244 2428-2428/com.example.speechtotext I/Process: Sending signal. PID: 2428 SIG: 9 
08-08 01:30:47.594 15145-15145/com.example.speechtotext W/System: ClassLoader referenced unknown path: /data/app/com.example.speechtotext-1/lib/x86 
08-08 01:30:49.125 15145-15145/com.example.speechtotext W/System: ClassLoader referenced unknown path: /data/app/com.example.speechtotext-1/lib/x86 
08-08 01:30:49.257 15145-15145/com.example.speechtotext W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
08-08 01:30:49.333 15145-15193/com.example.speechtotext D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 

                      [ 08-08 01:30:49.337 15145:15145 D/   ] 
                      HostConnection::get() New Host Connection established 0xac42d8c0, tid 15145 


                      [ 08-08 01:30:49.373 15145:15193 D/   ] 
                      HostConnection::get() New Host Connection established 0xae4929a0, tid 15193 
08-08 01:30:49.381 15145-15193/com.example.speechtotext I/OpenGLRenderer: Initialized EGL, version 1.4 
08-08 01:30:49.452 15145-15193/com.example.speechtotext W/EGL_emulation: eglSurfaceAttrib not implemented 
08-08 01:30:49.452 15145-15193/com.example.speechtotext W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaad5f60, error=EGL_SUCCESS 

Irgendwelche Ideen? Vielen Dank im Voraus! Seitennotiz: Ursprünglich habe ich die Fehlermeldung ausgelassen, weil es die if else-Anweisung innerhalb der OnActivityResult-Funktion war, die den Absturz/Fehler verursachte.

+0

Sie müssen die Fehlerausgabe posten – brandall

Antwort

1

Ihr Problem ist hier

wordBank = new ArrayList<>(R.array.Words); 

R.array.Words eine Integer-Konstante der Ressource selbst, nicht der Inhalt Ihrer Array.

Der richtige Weg, um einen XML-Array in Ihrem Code zu analysieren ist wie folgt:

String[] wordBank = getResources().getStringArray(R.array.Words); 

Wenn Sie these examples and a connected question sehen wollen, konvertieren String[] zu ArrayList<String>.

+0

Vielen Dank für Ihre Antwort und Beratung! – Brandon