Uncaught Handler错误。

时间:2013-03-21 10:23:29

标签: android android-handler

我正在尝试做一个HTTP Post,当我开始知道在异步任务或处理程序中应该使用耗时的任务时,我尝试使用处理程序对其进行编码但是我得到一个处理程序未捕获的异常,Unable确定我哪里错了。下面是我的代码和日志跟踪。

           @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);


    Button Bt = (Button)findViewById(R.id.button1);
    Button Btx = (Button)findViewById(R.id.button2);




    et = (EditText)findViewById(R.id.editText1);
    etp = (EditText)findViewById(R.id.editText2);

    Bt.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            dialog = ProgressDialog.show(MainActivity.this,"Loading", "Please Wait...");

            h = new Handler(){

                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    dialog.dismiss(); 
                }
            };


            new Thread(){

                @Override
                public void run() {
                    super.run();

                    Connection();

                    try {
                        Thread.sleep(3000);
                        h.sendEmptyMessage(0); 
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }

                }

            }.start();

        }
    });










   // Btx.setOnClickListener(Sig);




}





    private View.OnClickListener Sig =new View.OnClickListener() {
        public void onClick(View v){


            Intent Sign = new Intent(MainActivity.this,Signup.class);
            startActivity(Sign);

        }
        };









    public  void Connection(){
    String is = null;
    String X = et.getText().toString();
    String Y = etp.getText().toString();

    if(X.length()>0 && Y.length()>0){
        A = X;
        B = Y; 
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://animsinc.com/query.php"); 
        try {
           List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
           nameValuePairs.add(new BasicNameValuePair("username", X));
           nameValuePairs.add(new BasicNameValuePair("password",Y));
           httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           httpclient.execute(httppost);
        //   et.setText(""); // clear text box
        //   etp.setText(""); // clear text box

        HttpResponse responce = httpclient.execute(httppost);
        HttpEntity entity = responce.getEntity();
        is = EntityUtils.toString(entity);
       // in = entity.getContent();
      //  Log.e("post responce----->", "" + is);
        Toast.makeText(this,""+is, Toast.LENGTH_LONG).show();

         } catch (ClientProtocolException e) {
             // TODO Auto-generated catch block
         }catch (UnsupportedEncodingException e) {
            e.printStackTrace();
         }catch (IOException e) {
             // TODO Auto-generated catch block
         }




    } else{
            // display message if text fields are empty
             Toast.makeText(getBaseContext(),"All field are required",Toast.LENGTH_SHORT).show();
         }
    if(is != null){
        Toast.makeText(this,""+is, Toast.LENGTH_LONG).show();

    }



}

}

这是我的Log Cat:

enter image description here

1 个答案:

答案 0 :(得分:2)

问题是你在打电话:

Toast.makeText(this,""+is, Toast.LENGTH_LONG).show();

来自worker Thread的run()方法。你不应该这样做。您只能使用主(UI)线程来操作UI。

解决方案是使用runOnUiThread()AsyncTask或在Handler中显示Toast。