1

Brief Description of my Application so far:

The user will say a word the word will be saved into an ArrayList result, then it will compare to the words in the string array which is stored in the wordBank.xml file. If the word matches a word in the wordbank then it will display the word "match" in a textview and if not then it will display "no match".

Problem: The issue is comparing the two arraylist, within my onActivityResult function. It is a runtime error, this screen crashes when i try to start it, but when i change the code so it does not compare it will run without an error.

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 Function

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: Here is the error message:

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

Any ideas? Thank you in advance! Side note: Originally i left the error message out because it was the if else statement within the OnActivityResult function that caused the crash/error.

1
  • You need to post the error output Commented Aug 8, 2016 at 15:54

1 Answer 1

1

Your problem is here

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

R.array.Words is an integer constant of the resource itself, not the content of your array.

The correct way to parse an XML array in your code is as follows:

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

If you want to convert String[] to ArrayList<String> see these examples and a connected question.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for your answer and advice!
how to compare with similar words like comparing 'second' with '2nd'?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.