ProgressDialog不是在外部的AsyncTask工作
-
21-09-2019 - |
题
我开始以为得到一个ProgressDialog上班的AsyncTask必须是一个Activity类中的内部类。真正? [编辑很久以后...答案是假,我不知道这是一个错误或什么。我使用的是Android 1.5。我要在服务读了。
我有一个活性的使用数据库来操纵信息。如果填充数据库一切都很好。如果没有填充,然后我需要从网站上下载信息,填充数据库,然后访问填充的数据库来完成视图中的onCreate。
但问题是没有一些方法来确定什么时候的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被标记为一个错误: 类型活性的无外围实例是在范围访问
我想我需要FillDB延伸活动,然后创建FillDB延伸的AsyncTask内的私有类?这不会”工作。不能保证当FillDB活动将启动,不能使用startActivityForResult,因为没有结果从的AsyncTask完成时,它回来了。
更新:尝试创建在调用类的私有类。仍然无法显示ProgressDialog。其中一个错误是:无法添加窗口 - 令牌null不是一个应用程序。我不知道令牌被提及。
解决方案 2
我放弃了的AsyncTask和所使用的处理程序来处理线程。所有这些都是好的。
如果这是一个错误在1.5与的AsyncTask或别的东西我没做不知道。
其他提示
你有没有考虑重写onCreateDialog()方法来实现您的对话框?可以很容易地创建和控制他们这种方式Android提供大部分的工作适合你。 下面我有做你想要做的正是一个例子。
我具有用于与公共的(外部)的AsyncTask相当一段相同的问题OP。当错误信息研究“试图用非应用程序令牌添加窗口”,我发现问题是在设置是在的AsyncTask的构造函数传递的上下文。应传递的背景是“本”,不是this.getApplicationContext()或this.getBaseContext()。这解决了我的问题。
尝试运行
ProgressDialog progressDialog = new ProgressDialog(context);
//crashes with the following line
progressDialog.show(context, "Working..", "Retrieving info");
作为一个可运行的参数Activity.runOnUiThread()。用户界面元素应该被创建并在UI线程操纵。我想这也是Dialog
s如此。
[编辑] 代码:
Activity.this.runOnUiThread(new Runnable() {
public void run() {
ProgressDialog progressDialog = new ProgressDialog(context);
//crashes with the following line
progressDialog.show(context, "Working..", "Retrieving info");
}
};