如何将语音到文本API中的文本与ArrayList / String数组中的文本进行比较?

时间:2016-08-08 05:12:22

标签: android string arraylist speech-to-text

到目前为止我的申请简要说明:

用户会说一个单词将该单词保存到ArrayList结果中,然后它将与存储在wordBank.xml文件中的字符串数组中的单词进行比较。如果单词与wordbank中的单词匹配则会在textview中显示单词“match”,如果没有则会显示“no match”。

问题:问题是在我的onActivityResult函数中比较两个arraylist。这是一个运行时错误,当我尝试启动它时此屏幕崩溃,但是当我更改代码以使其无法比较时,它将运行而没有错误。

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

修改: 以下是错误消息:

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语句导致崩溃/错误。

1 个答案:

答案 0 :(得分:1)

你的问题在这里

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

R.array.Words是资源本身的整数常量,而不是数组的内容。

在代码中解析XML数组的正确方法如下:

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

如果您想将String[]转换为ArrayList<String>,请参阅these examples and a connected question