【问题标题】:ProgressDialog not working in external AsyncTaskProgressDialog 在外部 AsyncTask 中不起作用
【发布时间】:2010-03-21 16:01:52
【问题描述】:

我开始认为要让 ProgressDialog 工作,AsyncTask 必须是 Activity 类中的内部类。真的? [稍后编辑......答案是错误的,我不确定这是一个错误还是什么。我使用的是安卓 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 被标记为错误: Activity 类型的封闭实例在范围内是不可访问的

我想我需要 FillDB 扩展 Activity,然后在 FillDB 中创建一个私有类来扩展 AsyncTask?那行不通。无法保证 FillDB 活动何时启动并且不能使用 startActivityForResult,因为 AsyncTask 完成后没有返回任何结果。

更新:尝试在调用类中创建一个私有类。仍然无法显示 ProgressDialog。错误之一是:无法添加窗口——令牌 null 不适用于应用程序。我不知道指的是什么令牌。

【问题讨论】:

  • 在 Eclipse 中使用 adb logcat、DDMS 或 DDMS 透视图查看与强制关闭对话框相关的 Java 堆栈跟踪,因为该堆栈跟踪应该为您提供有关问题所在的更多信息.
  • CommonsWare - 我对 adb 的了解不够,无法在 /data/anr/traces.txt 中找到堆栈跟踪。但是查看 Eclipse 中的 Logcat 和 DDMS 似乎我有一个未捕获的运行时异常。 java.lang.reflect.InvocationTarget 异常 java.lang.Runtime 异常 WindowMai 尝试使用非应用程序令牌 WindowToken (43711b88 token=null) 添加窗口正在中止。我查看了上下文是否为空,但它似乎不是:this.context 显示:上下文 android.app.Application (id=830060323048)
  • 您传递给 AsyncTask 的上下文是什么?是 Activity 还是别的什么?
  • Romain,我正在传递应用程序上下文: context = getApplicationContext(); if(task == null || task.getStatus().equals(AsyncTask.Status.FINISHED)) { task = new FillNYLottoDB(context);任务.执行(空); }
  • 如果在任务运行时在横向/纵向模式之间切换,对话框会发生什么情况?使用 runOnUiThread(...) 在 Runnable 中创建对话框是否保存?

标签: android android-asynctask


【解决方案1】:

您是否考虑过重写 onCreateDialog() 方法来实现您的对话框?以这种方式创建和控制它们很容易,因为 Android 会为您完成大部分工作。 我在下面有一个示例,完全符合您的要求。

http://pastie.org/880540

【讨论】:

  • 杰森,那也没用。也许我还有其他事情发生?您运行的是哪个 Android 版本?
【解决方案2】:

我放弃了 AsyncTask 并使用 Handler 来处理线程。一切都很好。

不确定这是否是 1.5 中的 AsyncTask 中的错误或我没有做的其他事情。

【讨论】:

    【解决方案3】:

    很长一段时间以来,我在使用公共(外部)AsyncTask 时遇到了与 OP 相同的问题。在研究“尝试添加带有非应用程序令牌的窗口”的错误消息时,我发现问题在于设置在 AsyncTask 的构造函数中传递的上下文。应该传递的上下文是“this”,而不是 this.getApplicationContext() 或 this.getBaseContext()。这解决了我的问题。

    【讨论】:

      【解决方案4】:

      尝试运行

         ProgressDialog progressDialog = new ProgressDialog(context);
         //crashes with the following line
         progressDialog.show(context, "Working..", "Retrieving info");
      

      作为 Activity.runOnUiThread() 的可运行参数。用户界面元素应该在 UI 线程中创建和操作。我想Dialogs 也是如此。

      [编辑] 代码:

      Activity.this.runOnUiThread(new Runnable() {
        public void run() {
          ProgressDialog progressDialog = new ProgressDialog(context);
          //crashes with the following line
          progressDialog.show(context, "Working..", "Retrieving info");
        }
      };
      

      【讨论】:

      • Snowflake 先生,(如何格式化 cmets 以使所有内容不一起运行?)我试过了:Activity.runOnUiThread((Runnable) progressDialog.show(context, "Working..", "Retrieving info" ); 它告诉我:不能从 Activity 类型对非静态方法 runOnUIThread(Runnable) 进行静态引用。
      • MrSnowflake - 试过但不行。我编辑了我的帖子以显示您的代码并入我的。谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-11-20
      • 1970-01-01
      • 2012-08-13
      • 2015-04-15
      • 1970-01-01
      相关资源
      最近更新 更多