0

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

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


}
ffrstar777
  • 75
  • 2
  • 10

3 Answers3

3

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.

Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • 1
    You have a context in this line of your code - this.context = context; – Lalit Poptani Aug 26 '11 at 08:31
  • I added this: `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(); } };` but the dialog doesn't show? – ffrstar777 Aug 26 '11 at 08:53
  • 1
    This seems correct but you just for to start() the thread at last, do it. – Lalit Poptani Aug 26 '11 at 09:00
1

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.

Bugs
  • 4,491
  • 9
  • 32
  • 41
Sukirti Dash
  • 119
  • 1
  • 5
1

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();
               }
            });
Ron
  • 24,175
  • 8
  • 56
  • 97
  • Is this correct? `mHandler.postDelayed(new Runnable() { public void run() { ((Activity) context).runOnUiThread( new Runnable() { public void run() { // Dismiss the Dialog mDialog.dismiss(); } }); } }, 5000);` – ffrstar777 Aug 26 '11 at 08:33
  • I debugged it and found that the app crashes at mDialog.show() with the same looper.prepare error. Is there a way to use handler for show()? – ffrstar777 Aug 26 '11 at 08:54
  • 1
    show() also should be done in `runOnUIThread()` One for show() and one for dismiss(). – Ron Aug 26 '11 at 08:58
  • How do I get the thread to sleep for 5 seconds? It seems like the 5000 at the 2nd argument of postDelayed isn't working. I don't see any dialog popup. Here is my code: `mHandler.postDelayed(new Runnable() { public void run() { ((Activity) context).runOnUiThread( new Runnable() { public void run() { mDialog.show(); //SystemClock.sleep(5000); mDialog.dismiss(); } }); } }, 5000);` – ffrstar777 Aug 26 '11 at 09:03
  • 1
    You dont need to run another thread. Just run show dilog and dismiss dialog on UI thread. No need to start a new thread. – Ron Aug 26 '11 at 09:15