我开始认为,要使ProgressDialog起作用,AsyncTask必须是Activity类中的一个内部类。真的? [稍后编辑...答案为False,我不确定这是错误还是什么。我正在使用Android 1.5。我将阅读有关服务的内容。]
我有一个 Activity ,该 Activity 使用数据库来操纵信息。如果填充数据库,一切都会很好。如果未填充,则需要从网站下载信息,填充数据库,然后访问填充的数据库以完成onCreate中的 View 。
问题是没有任何方法可以确定AsyncTask线程何时完成了数据库的填充,我收到以下“强制关闭”错误消息:对不起!该应用程序意外停止。我单击“强制关闭”按钮,后台AsyncTask线程继续工作,填充了数据库,并且一切正常。
我需要摆脱该错误消息,并需要一些有关如何执行此操作的帮助。这是一些伪代码:
public class ViewStuff extends Activity
{
onCreate
{
if(database is populated)
do_stuff
else
{
FillDB task = null;
if(task == null || task.getStatus().equals(AsyncTask.Status.FINISHED))
{
task = new FillDB(context);
task.execute(null);
}
}
continue with onCreate using information from database to properly display
} // end onCreate
} // end class
在单独的文件中:
public class FillDB extends AsyncTask<Void, Void, Void>
{
private Context context;
public FillDB (Context c) //pass the context in the constructor
{
context = c;
}
public void filldb ()
{
doInBackground();
}
@Override
protected void onPreExecute()
{
ProgressDialog progressDialog = new ProgressDialog(context);
//crashes with the following line
progressDialog.show(context, "Working..", "Retrieving info");
}
@Override
protected Void doInBackground(Void... params)
{
// TODO Auto-generated method stub
try
etc etc etc
}
}
这是来自仿真器的堆栈跟踪:
----- pid 846 at 2010-03-21 19:58:25 -----
Cmd line: com.trial
DALVIK THREADS:
"main" prio=5 tid=3 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x40018e70
| sysTid=846 nice=0 sched=0/0 handle=-1098855268
at android.os.BinderProxy.transact(Native Method)
at android.app.ActivityManagerProxy.handleApplicationError(ActivityManagerNative.java:2103)
at com.android.internal.os.RuntimeInit.crash(RuntimeInit.java:302)
at com.android.internal.os.RuntimeInit$UncaughtHandler.uncaughtException(RuntimeInit.java:75)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:887)
at java.lang.ThreadGroup.uncaughtException(ThreadGroup.java:884)
at dalvik.system.NativeStart.main(Native Method)
"Binder Thread #3" prio=5 tid=15 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x43733d88
| sysTid=852 nice=0 sched=0/0 handle=1486928
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #2" prio=5 tid=13 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x437313c8
| sysTid=851 nice=0 sched=0/0 handle=1492472
at dalvik.system.NativeStart.run(Native Method)
"Binder Thread #1" prio=5 tid=11 NATIVE
| group="main" sCount=1 dsCount=0 s=0 obj=0x4372b9b0
| sysTid=850 nice=0 sched=0/0 handle=1492664
at dalvik.system.NativeStart.run(Native Method)
"JDWP" daemon prio=5 tid=9 VMWAIT
| group="system" sCount=1 dsCount=0 s=0 obj=0x4372a2a0
| sysTid=849 nice=0 sched=0/0 handle=1490176
at dalvik.system.NativeStart.run(Native Method)
"Signal Catcher" daemon prio=5 tid=7 RUNNABLE
| group="system" sCount=0 dsCount=0 s=0 obj=0x4372a1e8
| sysTid=848 nice=0 sched=0/0 handle=1487888
at dalvik.system.NativeStart.run(Native Method)
"HeapWorker" daemon prio=5 tid=5 VMWAIT
| group="system" sCount=1 dsCount=0 s=0 obj=0x427d03c0
| sysTid=847 nice=0 sched=0/0 handle=1487592
at dalvik.system.NativeStart.run(Native Method)
----- end 846 -----
我究竟做错了什么?
雪花先生
尝试过:
@Override
protected void onPreExecute()
{
Activity.this.runOnUiThread(new Runnable() {
public void run() {
ProgressDialog progressDialog = new ProgressDialog(context);
//crashes with the following line
progressDialog.show(context, "Working..", "Retrieving info");
}
});
}
Activity.this被标记为错误:
作用域中没有可访问的 Activity 类型的封闭实例
我在想我需要FillDB扩展Activity,然后在FillDB中创建一个扩展AsyncTask的私有(private)类吗?那行不通。无法保证FillDB Activity 何时开始,并且不能使用startActivityForResult,因为AsyncTask完成时不会返回任何结果。
更新:尝试在调用类中创建一个私有(private)类。仍然无法显示ProgressDialog。错误之一是:无法添加窗口- token null不适用于应用程序。我不知道要指代什么 token 。
最佳答案
您是否考虑过重写onCreateDialog()方法来实现您的对话框?以这种方式创建和控制它们很容易,因为Android可以为您完成大部分工作。
我有一个下面的示例完全按照您的 Intent 进行。
http://pastie.org/880540
关于android - ProgressDialog在外部AsyncTask中不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2487690/