开发者

display progressdialog in non-activity class

开发者 https://www.devze.com 2023-03-31 11:48 出处:网络
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.RuntimeExcepti

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.

Here is some of my code:

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开发者_StackOverflow中文版);

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


}


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

context.this.runOnUiThread(new Runnable() {

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

            }
        });

Hope this works.


You do this

  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();
               }
            });


This is the exception you will get if you are performing any UI operation on any thread or from any background task. Also context.runOnUiThread won't work.

Instead use:

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

You can use the same for showing where activity is the object of activity.

0

精彩评论

暂无评论...
验证码 换一张
取 消