问题
Im Adding Progress Dialog in some Activity .But im getting Exception mention in title.how to resolve it.
dialog = ProgressDialog.show(Notification.this, "loading please wait",
"Loading. Please wait...", true);
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
dialog.dismiss();
}
}.start();
Any thing wrong with this.all Background process is performed in performbackgroundprocess method.
回答1:
You cant call dialog.dismiss(); in the background thread. You can make Threads send messages to handlers when they are done and in the handler you can dismiss the dialog. Handlers work in ui thread
There is a tutorial about it
回答2:
use runOnUiThread as:
new Thread() {
public void run() {
try{
performBackgroundProcess1();
//sleep(3000,000);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
// dismiss the progress dialog
CurrentActivity.this.runOnUiThread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
}
}.start();
来源:https://stackoverflow.com/questions/10497854/only-the-original-thread-that-created-a-view-hierarchy-can-touch-its-views