如何将 Speech To Text API 中的文本与 ArrayList/String 数组中的文本进行比较?

How to compare Text from Speech To Text API to text in a ArrayList/String Array?

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

用户说出一个词,这个词将被保存到一个ArrayList结果中,然后它会与存储在wordBank.xml文件中的字符串数组中的词进行比较。如果单词与词库中的单词匹配,那么它将在文本视图中显示单词 "match",如果不匹配,则将显示 "no match".

问题:问题是在我的 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);
}

编辑: 这是错误消息:

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 语句导致 crash/error.

你的问题就在这里

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