Reputation:
In my android app, I am trying to make use of ASYNC Task ... whenever I set text of any TextView of EditText in doInBackground(...) .... I get an error , but when I set the same in onCreate , it works .. :( here is the code :
public class RemoteFiles extends Activity {
EditText etrmm ;
TextView t;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_remote_files);
initVars();
new ConnectToServer().execute(null);
}
protected void initVars(){
etrmm =(EditText)findViewById(R.id.etRM);
etrmm.setText("UnSET");
t=(TextView)findViewById(R.id.RM);
t.setText("UnsET");
}
public class ConnectToServer extends AsyncTask<String, Integer, Void> {
//private ProgressDialog pd = new ProgressDialog(RemoteFiles.this);
private ProgressDialog pdd ;
@Override
protected Void doInBackground(String... params) {
// TODO Auto-generated method stub
for(int i=0;i<20;i++){
publishProgress(5);
try {
Thread.sleep(88);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
etrmm.setText("Edittext SET");
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
pdd.dismiss();
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// pd.setMessage("Connecting to server... ");
// pd.show();
pdd = new ProgressDialog(RemoteFiles.this);
pdd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pdd.setMax(100);
pdd.show();
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
pdd.incrementProgressBy(values[0]);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_remote_files, menu);
return true;
}}
here is the layout :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/RM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="@dimen/padding_medium"
android:text="@string/hello_world"
tools:context=".RemoteFiles" />
<EditText
android:id="@+id/etRM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" >
<requestFocus />
</EditText>
logcat says android.view.ViewRoot$CalledFromWrongThreadException
Upvotes: 0
Views: 419
Reputation: 133560
AsyncTask's onpreExecute and onPostExecute methods run o UI Thread. You can make changes to the UI here. doInBackground does not run on UI thread. It is used to do long Running operations. Runs in the background thread. You cannot update ui from the background thread in doinBackground.
Set text in textview in onPostExecute method of asynctask.
The document in the developer site has detailed information regarding the topic under the heading THE 4 STEPS. http://developer.android.com/reference/android/os/AsyncTask.html
Upvotes: 0
Reputation: 20155
doInBackground(String... params) not run on UI Thread, so you can't set text for edittext there
if you want to set edittext value, return the value in doInBackGround and use the parameter value in onPostExecute and set it in onPostExecute()
Upvotes: 1