I start a thread in activity. There is a constructor textview in thread. It has no error of setText when update the textview in run.
But if I new a thread without parameter, then setTextView (TextView) and setText() in run.. There is the error.
Why it can appear this?
Edit: My code
package extthread;
import android.widget.TextView;
public class MyUpdateUIThread extends Thread {
private TextView textView1;
public MyUpdateUIThread(TextView textView1) {
super();
this.textView1 = textView1;
}
@Override
public void run() {
super.run();
textView1.setText("zzzzzzzzzzzzzz");
}
}
------------------------------
package testtest.test.run;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import extthread.MyUpdateUIThread;
public class Main extends Activity {
TextView textView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView) this.findViewById(R.id.textView1);
Thread thread = new Thread(new MyUpdateUIThread(textView));
thread.start();
}
};
Use runOnUiThread inside run method of Thread for updating TextView as:
EDIT :
as in your current code Thread is from separate class . then you will need to pass Main Activity instance to Thread class to access
runOnUiThreadinMyUpdateUIThreadthread . change yourMyUpdateUIThreadclass constructor as:and create
MyUpdateUIThreadobject in Main Activity as: