Android Thread Not Working
In this code snippet, the application sleeps for the interval, but instead of appending TEXT to textStatus(TextView variable), it displays an error that Something went wrong and ap
Solution 1:
To update the UI you can also use Handler like this, it will update your UI incrementing the value of counter everytime by 1 within every second.
intresponse=0;
publicvoidonCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView)findViewById(R.id.myid);
threadt=newthread();
t.start();
}
publicclassthreadextendsThread {
publicvoidrun() {
while (response < 100) {
handler.sendEmptyMessage(0);
response++;
try {
sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
privateHandlerhandler=newHandler() {
publicvoidhandleMessage(Message msg) {
tv.setText("helloworld " + response);
}
};
Solution 2:
all the changes to the UI Part should be done on the UIThread, you should use something like post
, or runOnUithread
functions to update UI.
Post a Comment for "Android Thread Not Working"