0

Possible Duplicate:
Can’t create handler inside thread that has not called Looper.prepare()

I want to call the alertdialogbox in the thread as follows

    public class connect implements Runnable
     {
     public void run()  //run method

    {

        AlertDialog.Builder alert = new AlertDialog.Builder(cordovaExample.activity);

        alert.setTitle("Confirm");
        alert.setMessage("Are you sure?");

        alert.setPositiveButton("YES", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Do nothing but close the dialog

                dialog.dismiss();
            }

        });

        alert.setNegativeButton("NO", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
                // Do nothing
                dialog.dismiss();
            }
        });

        AlertDialog s = alert.create();
        alert.show();
       }
     }

I am calling this thread from the following activity

public class cordovaExample extends DroidGap
{
    Context mcontext;

    public static cordovaExample activity;
private static MediaPlayer  music=null;
    @Override
    public void onCreate(Bundle savedInstanceState)

        {
            super.onCreate(savedInstanceState);

            activity=this;

                             super.init();



                   new Thread(new connect(this)).start();

                     }
                }

but its giving error Can't create handler inside thread that has not called Looper.prepare()

Any help will be appreciated

greenfxpips.com
  • 200
  • 1
  • 3
  • 12

2 Answers2

2

Write this line in App UI thread

 alert.show();

Something like this

MainActivity.this.runOnUiThread(new Runnable() {

        public void run() {
              alert.show();

        }
    });
Ali Imran
  • 8,927
  • 3
  • 39
  • 50
1

you cant do changes in ui while thread is running.If u want to do so,Use Handler or runOnUiThead or best option is to use AsyncTask.

Richa
  • 3,165
  • 1
  • 22
  • 26