[英]Black Screen when trying to dismiss ProgressDialog inside a Thread
The first problem was that the ProgressDialog was not showing.第一个问题是 ProgressDialog 没有显示。 Then I read that I should create a Thread.
然后我读到我应该创建一个线程。 I created the Thread and it worked.
我创建了线程并且它起作用了。 But now I can't dismiss the ProgressDialog.
但现在我不能关闭 ProgressDialog。 The app crashes, the screen goes black.
应用程序崩溃,屏幕变黑。
xGrava.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ProgressDialog PD = ProgressDialog.show(NovaOcc.this,"","");
PD.setContentView(R.layout.ipp_load);
PD.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Thread mThread = new Thread() {
@Override
public void run() {
if (ValidaDados() == true) {
if (GravaDados() == true) {
xGrava.setVisibility(View.GONE);
PD.dismiss();
}
}
}
};
mThread.start();
}
});
As I see, I guess it is for Android. Generally, we use Threads when there is a long process to run, and we don't want let the view UI stuck because it is a UI Thread running.我看到的,我猜是Android的。一般我们在有很长的进程要运行的时候使用Threads,我们不希望让view UI卡死,因为它是一个UI Thread在运行。
So to answer the question, please make sure when calling any action on view, to do it on "runOnUIThread"所以要回答这个问题,请确保在调用视图上的任何操作时,在“runOnUIThread”上执行
For example here, once your Thread finishes the "long" task, you can do:例如这里,一旦你的线程完成了“长”任务,你可以这样做:
xGrava.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final ProgressDialog PD = ProgressDialog.show(NovaOcc.this,"","");
PD.setContentView(R.layout.ipp_load);
PD.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
Thread mThread = new Thread() {
@Override
public void run() {
if (ValidaDados() == true) {
if (GravaDados() == true) {
runOnUiThread(new Runnable() {
@Override
public void run() {
xGrava.setVisibility(View.GONE);
PD.dismiss();
}
}
);
}
}
}
};
mThread.start();
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.