【问题标题】:display progressdialog in non-activity class在非活动类中显示进度对话框
【发布时间】:2011-08-26 08:09:00
【问题描述】:

我正在尝试在非 Activity 类中显示对话框。基本上,我在我的应用程序中检测到一个对象,我想显示一个对话框然后切换活动。我在我的 logcat 中收到“java.lang.RuntimeException:无法在未调用 Looper.prepare() 的线程内创建处理程序”。

这是我的一些代码:

public ImageTargetsRenderer(Context context) {
    this.context = context;
    mDialog = new ProgressDialog(context);
  }

public void onDrawFrame(GL10 gl) {
    testFlag = 0;

    // DO NOT RENDER IF THERE IS NO TRACKABLE
    if (!mIsActive)
        return;

    // Call our native function to render content
    // RENDER IF THERE IS A TRACKABLE
    testFlag = renderFrame();

    System.err.println("ImageTargetsRenderer reports: " + testFlag);

    if(testFlag > 0 && frameCount > 5)
    {
        frameCount = 0;
        System.err.println("Starting to switch activities.");

        mDialog.setTitle("Please wait");
        mDialog.setMessage("Please wait");
        mDialog.show();

        new Thread() {
            public void run() {
                        try{
                            sleep(5000);
                        } catch (Exception e) { }
                // Dismiss the Dialog
                mDialog.dismiss();
            }
        }.start();


        Intent myIntent = new Intent(context, FlashActivity.class);
        myIntent.putExtra("com.qualcomm.QCARSamples.ImageTargets.flagTest", testFlag);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        context.startActivity(myIntent);
        testFlag = 0; 

        return;
    }
    frameCount++;


}

【问题讨论】:

  • 添加我的编辑后更新有问题的代码..

标签: android android-activity dialog


【解决方案1】:

应该从 UIthread 调用您的 Dialog,因此请尝试使用它,

context.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                mDialog.show();

            }
        });

希望这行得通。

【讨论】:

  • 你在这行代码中有一个上下文 - this.context = context;
  • 我添加了这个:new Thread() { public void run() { ((Activity) context).runOnUiThread(new Runnable() { @Override public void run() { System.err.println("SHOW UP!"); mDialog.show(); } }); try { sleep(5000); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } mDialog.dismiss(); } }; 但对话框没有显示?
  • 这似乎是正确的,但你只是为了启动()线程最后,去做吧。
【解决方案2】:

你这样做

  context.runOnUIThread( new Runnable() {
             public void run() {
                  // show the Dialog
                  mDialog.setTitle("Please wait");
                  mDialog.setMessage("Please wait");

                  mDialog.show();
               }
            });
         }

     Thread.sleep(5000);

        context.runOnUIThread( new Runnable() {
             public void run() {
                  // Dismiss the Dialog
                  mDialog.dismiss();
               }
            });

【讨论】:

  • 这是正确的吗? mHandler.postDelayed(new Runnable() { public void run() { ((Activity) context).runOnUiThread( new Runnable() { public void run() { // Dismiss the Dialog mDialog.dismiss(); } }); } }, 5000);
  • 我调试了它,发现应用程序在 mDialog.show() 处崩溃并出现相同的 looper.prepare 错误。有没有办法使用 show() 的处理程序?
  • show() 也应该在runOnUIThread() 中完成,一个用于show(),一个用于dismiss()。
  • 如何让线程休眠 5 秒?似乎 postDelayed 的第二个参数的 5000 不起作用。我没有看到任何对话框弹出。这是我的代码:mHandler.postDelayed(new Runnable() { public void run() { ((Activity) context).runOnUiThread( new Runnable() { public void run() { mDialog.show(); //SystemClock.sleep(5000); mDialog.dismiss(); } }); } }, 5000);
  • 你不需要运行另一个线程。只需在 UI 线程上运行 show dilog 并关闭对话框。无需启动新线程。
【解决方案3】:

如果您在任何线程上或从任何后台任务执行任何 UI 操作,则会出现这种异常。 context.runOnUiThread 也不起作用。

改为使用:

activity.runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                             mDialog.dismiss();
                            }
                          });

您可以使用它来显示activityactivity 的对象的位置。

【讨论】:

    猜你喜欢
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多