0
Hello, I made an app in Android Studio that breaks the phrases in words and stores in an array using split. Then I made a loop of repetition using while so that it displays word for word, but there was a problem, the application would freeze the screen, and after having done the whole loop would update it and only appeared the last word of the vector. To solve this I used a Thread, solved, it updates, but the app hangs when it ends. Can someone 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.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)
This Sleep of yours, what value is there in wpm? Maybe it’s going a high value and you think the app is crashing
– Woton Sampaio
Put a Log to see what is the value of this wpm
– Woton Sampaio
This WPM is for minutes, the person informs as much of words per minute, and I turn there to milliseconds. And every word he shows, he gives an Sleep in milliseconds. The problem is not that, the application now runs normal, only that after it finishes displaying all the vector words slowly the application closes. " Application stopped." on mobile.
– Vinicius Petrachin
Oh yeah, it stops working, I thought it just got stuck, goes in the logcat and copies here what the error, there it shows, it is easier to help
– Woton Sampaio
I added the logcat to the question, att.
– Vinicius Petrachin