2016-08-08 9 views
1

これまでのところ、Speech To Text APIのテキストとArrayList/String Arrayのテキストを比較するには?

ユーザは、単語がArrayList結果に保存されると言うと、wordBank.xmlファイルに格納されている文字列配列の単語と比較します。単語が単語バンクの単語と一致する場合は、テキストビューに単語「一致」を表示し、一致しない場合は「一致しません」と表示します。

問題:問題は、2つのarraylistを、私のonActivityResult関数内で比較しています。ランタイムエラーです。この画面は起動しようとするとクラッシュしますが、コードを変更してエラーが発生しても実行されません。

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機能

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:

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 

任意のアイデア:ここ はエラーメッセージのですか?前もって感謝します! サイドノート:元々は、クラッシュ/エラーの原因となったOnActivityResult関数内のif elseステートメントであったため、エラーメッセージが出ました。

+0

エラー出力を投稿する必要があります – brandall

答えて

1

あなたの問題はここに

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

R.array.Wordsでは、リソース自体の整数定数ではなく、あなたの配列の内容です。次のように

あなたのコード内のXML列を解析するための正しい方法は次のとおりです。

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

あなたがthese examples and a connected questionを参照してくださいString[]ArrayList<String>に変換したい場合。

+0

あなたの回答とアドバイスをありがとう! – Brandon

関連する問題