安卓:ProgressDialog失败 [英] Android: ProgressDialog failing
问题描述
我在得到一个ProgressDialog启动和运行的实际问题。我的code:
I'm having real problems getting a ProgressDialog up and running. My code:
ProgressDialog dialog;
try {
dialog = new ProgressDialog(context);
dialog.setCancelable(true);
dialog.setMessage("Loading ...");
// set the progress to be horizontal
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// set the bar to the default value of 0
dialog.setProgress(0);
// set the maximum value
dialog.setMax(100);
// display the progressbar
dialog.show();
}
catch (Exception e1)
{
e1.printStackTrace();
}
下面,我创建我的后台线程加载一些东西,更新进度条,但它从来没有得到那么远。在堆栈跟踪我得到无法添加窗口 - 令牌null不是一个应用程序,但似乎对话(在调试器)拥有所有正确的事情,它不是空的,但我得到这个错误。
Below that I create my background thread to load some stuff and update the progress bar, but it never gets that far. In the stack trace I get "Unable to add window -- token null is not for an application", but the dialog seems (in the debugger) to have all the right things, it isn't null, but I'm getting this error.
任何人都可以照这个光?
Can anyone shine a light on this?
推荐答案
我认为ProgressDialog想用ApplicationContext的工作。这个API是不是很正确的构造应要求一个活动,而不是一个语境。
I think that the ProgressDialog want work with a ApplicationContext. The API is not very correct the constructor should request an Activity instead of a Context.
尝试一个活动的引用传递给构造函数,而不是一个常规的上下文。
Try to pass a reference to a Activity to the constructor instead of an regular Context.
而不是使用的:
Context context = getApplicationContext();
使用
Context context = this;
或者,如果你是在一个内部类的活动(侦听器或任务)的使用:
Or if you are in an inner class of your Activity (a listener, or Task) use:
Context context = MyActivityNameComesHere.this;
请参阅我的问题在Android的错误追踪系统这一点。
See my Issue in the Android Bugtracker about this.
这篇关于安卓:ProgressDialog失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!