Reputation: 29
I'm trying to create a thread that keeps updating a TextView while the Main activity is running
I have used the Runnable class but it doesn't work Every time my app crashes ...
Is there any way to do that ?? Please help
Upvotes: 2
Views: 448
Reputation: 21
If you want to keep updating the UI regularly from a runnable, you could start an AsyncTask and update the TextView from postExecute function. postExecute always runs on the UI thread.
You can find more information here:
https://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 2
Reputation: 3237
You may need this in your activity:
runOnUiThread(new Runnable() {
@Override
public void run() {
textView.setText("something.");
}
});
or this:
textView.post(new Runnable() {
@Override
public void run() {
textView.setText("something.");
}
});
Both of these can let you change UI in work thread.
Upvotes: 3
Reputation: 1951
Android doesn't allow you to change the UI from another thread. This is a OS design decision, meant to simplify UI handling. You can do whatever you want on secondary threads, but if you want to interact with views and widgets, you have to do it through the UI thread, as explained in the official documentation here.
Upvotes: 0