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)