安卓:ProgressDialog不显示 [英] Android: ProgressDialog doesn't show
问题描述
我试图创建一个ProgressDialog为Android的应用程序(只需一个简单的显示出的东西正在发生的事情,没有任何按键或任何用户),但我无法得到它的权利。我已经通过论坛和教程,以及对样品 - code自带的SDK,但无济于事。
I'm trying to create a ProgressDialog for an Android-App (just a simple one showing the user that stuff is happening, no buttons or anything) but I can't get it right. I've been through forums and tutorials as well as the Sample-Code that comes with the SDK, but to no avail.
这是我得到了什么:
btnSubmit.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
(...)
ProgressDialog pd = new ProgressDialog(MyApp.this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage("Working...");
pd.setIndeterminate(true);
pd.setCancelable(false);
// now fetch the results
(...long time calculations here...)
// remove progress dialog
pd.dismiss();
我也尝试添加 pd.show();
和混乱与周围的参数新ProgressDialog
导致什么都没有(除了选择的参数将无法正常工作的错误),这意味着:在ProgressDialog永远不会出现。该应用程序只是继续运行,就好像我从来没有加入该对话框。
I've also tried adding pd.show();
and messed around with the parameter in new ProgressDialog
resulting in nothing at all (except errors that the chosen parameter won't work), meaning: the ProgressDialog won't ever show up. The app just keeps running as if I never added the dialog.
我不知道如果我在正确的地方创建对话框,我把它绕了一下但也didnt't帮助。也许我在错误的环境?上述code是在私人的ViewGroup _createInputForm()
在的MyApp
。
I don't know if I'm creating the dialog at the right place, I moved it around a bit but that, too, didnt't help. Maybe I'm in the wrong context? The above code is inside private ViewGroup _createInputForm()
in MyApp
.
任何暗示是AP preciated,
Any hint is appreciated,
推荐答案
您必须调用pd.show长计算开始前,然后计算必须在一个单独的线程中运行。一个一旦该线程完成,你必须调用pd.dismiss()关闭prgoress对话框。
you have to call pd.show before the long calculation starts and then the calculation has to run in a separate thread. A soon as this thread is finished, you have to call pd.dismiss() to close the prgoress dialog.
在这里你可以看到一个例子:
here you can see an example:
在progressdialog创建并显示与一个线程调用运行一个沉重的计算:
the progressdialog is created and displayed and a thread is called to run a heavy calculation:
@Override
public void onClick(View v) {
pd = ProgressDialog.show(lexs, "Search", "Searching...", true, false);
Search search = new Search( ... );
SearchThread searchThread = new SearchThread(search);
searchThread.start();
}
和这里的主题:
private class SearchThread extends Thread {
private Search search;
public SearchThread(Search search) {
this.search = search;
}
@Override
public void run() {
search.search();
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
displaySearchResults(search);
pd.dismiss();
}
};
}
这篇关于安卓:ProgressDialog不显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!