1

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

In my program I want show progress-dialog, for that I have created thread, but it gives meCan't create handler inside thread that has not called Looper.prepare() I look previous posted question also but I can handle this . this is my code

        dialog=ProgressDialog.show(Login.this, "Connecting to the Server", "Please wait");

        Thread thred=new Thread(new Runnable() {

            public void run() {
                  try {


                      // Create a new HttpClient and Post Header
                        HttpClient httpclient = new DefaultHttpClient();           
                        HttpPost httppost = new HttpPost("http://www.diskonbanget.com/bni/login/login.php");
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                        nameValuePairs.add(new BasicNameValuePair("username", username));
                        nameValuePairs.add(new BasicNameValuePair("password", password));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        // Execute HTTP Post Request

                        HttpResponse response = httpclient.execute(httppost);

                        String str = inputStreamToString(response.getEntity().getContent()).toString();


                        if(str.toString().equalsIgnoreCase("false"))
                        {
                            dialog.dismiss();
                            AlertDialog alert = new AlertDialog.Builder(Login.this).create();                       
                            alert.setMessage("Please enter valid username & Password");
                            alert.setButton("OK",new DialogInterface.OnClickListener() {
                                        public void onClick(DialogInterface dialog,int which) {
                                        }
                                    });

                          alert.setIcon(R.drawable.loginlogo); 
                          alert.show();


                        }
                        else{   

                             Intent intent=new Intent(Login.this,MainMenu.class);
                             intent.putExtra("photo", str);
                             intent.putExtra("username", username);
                             txtusername.setText("");
                             txtPassword.setText("");
                             dialog.dismiss();
                             startActivity(intent);


                        }

                    } catch (Exception e) {
                        dialog.dismiss();
                        AlertDialog alert = new AlertDialog.Builder(Login.this).create();                       
                        alert.setMessage("Can not Connect to the server.please make sure your internet is Switch on");
                        alert.setButton("OK",new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,int which) {
                                    }
                                });
                        alert.setIcon(R.drawable.loginlogo); 

                        alert.show();
                    } 


            }
        });
        thred.start();

please someone help me to fix this problem.

Community
  • 1
  • 1
  • where is this code within your application? An activity, a service, your own POJO, elsewhere? The best way to send information to the UI depends on where you are... – Paul D'Ambra Oct 10 '12 at 22:45
  • this code is in my activity class –  Oct 11 '12 at 05:35

1 Answers1

0

A background thread cannot display a Dialog. Please have the main application thread do that, such as by using an AsyncTask instead of a raw thread and displaying the dialog in onPostExecute().

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491