我尝试在线程旁边显示ProgressDialog。但是当应用程序运行Progressdialog时会出现这种异常
行执行时android.view.WindowLeaked: 活动com.testApp.CaptureSignature已泄露窗口 com.android.internal.policy.impl.PhoneWindow $ {DecorView 528dd504 V.E ..... R .....我。最初添加的0,0-949,480}
错误获取
pDialog.show();
public void syncing(final int sel){
if(sel==1){
ProgressDialo pDialog = new ProgressDialog(CaptureSignature.this);
pDialog.setMax(100);
pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pDialog.setCancelable(false);
pDialog.setProgress(0);
pDialog.setOnDismissListener(new MyCustomDialog.OnDismissListener() {
@Override
public void onDismiss(final DialogInterface dialog) {
doaftersync(pDialog.getProgress(),sel);
}
});
pDialog.setMessage("Syncing Deliveries.Please wait..");
pDialog.show();
Thread background = new Thread (new Runnable() {
public void run() {
progressHandler.sendMessage(progressHandler.obtainMessage());
int stat = deliveryup();
if(stat==1){
try {
locationManager.removeUpdates(locationListner);
} catch (Exception e2) {
}
}else{
pDialog.dismiss();
return;
}
progressHandler.sendMessage(progressHandler.obtainMessage());
int isustat=issueup();
if(isustat==0){
pDialog.dismiss();
return;
}
progressHandler.sendMessage(progressHandler.obtainMessage());
int locstat=locationup();
if(locstat==0){
pDialog.dismiss();
return;
}
cleanup();
progressHandler.sendMessage(progressHandler.obtainMessage());
pDialog.dismiss();
return;
}
});
background.start();
}
}
// handler for the background updating
Handler progressHandler = new Handler() {
public void handleMessage(Message msg) {
pDialog.incrementProgressBy(25);
}
};
任何帮助.. !!
答案 0 :(得分:3)
使用ProgressDialog
或使用Handler
方法取消主线程中的runOnUiThread()
也许你得到了异常,因为当你的Activity
被销毁时,进度对话框正在运行。你应该在Activity
被销毁时解雇对话
答案 1 :(得分:2)
在UI线程中执行所有UI操作。
runOnUiThread(new Runnable() {
@Override
public void run() {
pDialog.dismiss();
}
});
答案 2 :(得分:1)
我认为安全的方式:
if(dialog.isShowing()){
dialog.dismiss();
}