Когда вы находитесь в рабочем потоке, вы не можете напрямую манипулировать элементами пользовательского интерфейса на Android.
Когда вы используете AsyncTask, пожалуйста, поймите методы обратного вызова.
Например:
public class MyAyncTask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
// Here you can show progress bar or something on the similar lines.
// Since you are in a UI thread here.
super.onPreExecute();
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// After completing execution of given task, control will return here.
// Hence if you want to populate UI elements with fetched data, do it here.
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
// You can track you progress update here
}
@Override
protected Void doInBackground(Void... params) {
// Here you are in the worker thread and you are not allowed to access UI thread from here.
// Here you can perform network operations or any heavy operations you want.
return null;
}
}
К сведению: чтобы получить доступ к потоку пользовательского интерфейса из рабочего потока, используйте либо метод runOnUiThread (), либо метод post в своем представлении.
Например:
runOnUiThread(new Runnable() {
textView.setText("something.");
});
or
yourview.post(new Runnable() {
yourview.setText("something");
});
Это поможет вам лучше узнать вещи.Следовательно, в вашем случае вам нужно установить текстовое представление в методе onPostExecute ().