【问题标题】:android:Can't create handler inside thread that has not called Looper.prepare() [duplicate]android:无法在未调用 Looper.prepare() 的线程内创建处理程序 [重复]
【发布时间】:2012-10-01 12:37:21
【问题描述】:

可能重复:
Can’t create handler inside thread that has not called Looper.prepare() inside AsyncTask for ProgressDialog

在我的程序中,我想显示进度对话框,因为我已经创建了线程,但它给了我Can't create handler inside thread that has not called Looper.prepare() 我也查看了以前发布的问题,但我可以处理这个问题。这是我的代码

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

请有人帮我解决这个问题。

【问题讨论】:

  • 此代码在您的应用程序中的什么位置?一项活动,一项服务,你自己的 POJO,在别处?向 UI 发送信息的最佳方式取决于您所在的位置...
  • 此代码在我的活动类中

标签: android multithreading handler looper


【解决方案1】:

后台线程无法显示Dialog。请让主应用程序线程执行此操作,例如使用AsyncTask 而不是原始线程并在onPostExecute() 中显示对话框。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-10-17
    • 1970-01-01
    • 1970-01-01
    • 2014-03-03
    • 2011-08-23
    • 2012-11-27
    相关资源
    最近更新 更多