App crashes after finishing loop in Thread

0

Hello, I made an app in Android Studio that breaks the sentences into words and stores them in a vector using the split. Then I made a loop of repetition using the while to display it word for word, but there was a problem, the application freezes the screen, and after having done all the loop it updated and ended up only appearing the last word of the vector. To solve this I used a Thread, resolved, it updates, however the app hangs when it finishes. Can anyone help me?

Code:

button.setOnClickListener(
                new View.OnClickListener(){
                    public void onClick(View view){
                        Thread t1 = new Thread(){
                            int wpm;
                            String frase;
                            int numbers;
                            int i;
                            @Override
                            public void run(){
                                numbers = Integer.parseInt(txt2.getText().toString());
                                wpm = 1000/(numbers/60);
                                frase = txt1.getText().toString();
                                final String palavras[] = frase.split(" ");
                                i = 0;
                                while(i++ < palavras.length){
                                    try {
                                        runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                label.setText(String.valueOf(palavras[i]));
                                            }
                                        });
                                        Thread.sleep(wpm);
                                    } catch (InterruptedException e) {
                                        e.printStackTrace();
                                    }
                                }
                            }
                        };
                        t1.start();
                    }
                }
        );

Logcat:

  

05-02 01: 33: 16.142 6738-6738 / com.exsapps.readfast E / AndroidRuntime:   FATAL EXCEPTION: main       Process: com.exsapps.readfast, PID: 6738       java.lang.ArrayIndexOutOfBoundsException: length = 122; index = 122           at com.exsapps.readfast.Main $ 1 $ 1 $ 1.run (Main.java:48)           at android.os.Handler.handleCallback (Handler.java:739)           at android.os.Handler.dispatchMessage (Handler.java:95)           at android.os.Looper.loop (Looper.java:148)           at android.app.ActivityThread.main (ActivityThread.java:7409)           at java.lang.reflect.Method.invoke (Native Method)           at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:1230)           at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

    
asked by anonymous 01.05.2018 / 21:26

2 answers

1

I read Carlos's comment, and tried and went wrong, after that, I added the loop control variable to be declared at the beginning of the code and in the loop I used only for (i = 0; i

02.05.2018 / 22:18
1

The problem should be while(i++ < palavras.length) that is, i is being compared and after incremented, so within the loop the final value of i will end up being palavras.length which is an invalid index.

I recommend using for as in (well conventional, every programmer will recognize):

for (int i = 0; i < palavras.length; i++) {
    final String palavra = palavras[i];
    ...
        label.setText(palavra);
    ...
}
    
02.05.2018 / 16:31