在非活动类中显示进度对话框 [英] display progressdialog in non-activity class

查看:26
本文介绍了在非活动类中显示进度对话框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to display a dialog in a non-Activity class. Basically, I detect an object in my app, I would like to display a dialog and then switch activities. I'm getting a "java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()" in my logcat.

这是我的一些代码:

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++;


}

推荐答案

你的 Dialog 应该从 UIthread 调用,所以尝试使用它,

Your Dialog should be called from the UIthread so try to use this,

context.this.runOnUiThread(new Runnable() {

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

            }
        });

希望这行得通.

这篇关于在非活动类中显示进度对话框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆