sinat_34047321
HappyTomatoes
2016-04-22 11:33

安卓中的问题,求教各位大神

  • android

安卓中 This Handler class should be static or leaks might occur
大家有什么好的解决方法吗

小白一枚,求教各位大神

如下buddle附近总是提示This Handler class should be static or leaks might occur

    button1 = (Button) findViewById(R.id.car_sub);

    button1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            progressDialog1 = ProgressDialog.show(MycarActivity.this, "",
                    "服务器连接中...", true, false);
            //  开启线程去查密码
            new Thread() {

                @Override
                public void run() {

                    // 向handler发消息
                    handler2.sendEmptyMessage(0);
                }
            }.start();
        }
    });             

   } 


private Handler handler2 = new Handler() {

    @SuppressLint("HandlerLeak")
    @Override
    public void handleMessage(Message msg) {
        // 查询操作

        register();

    }
};

private void register() {
    //获取传过来的用户名

    Bundle Bundle1 = this.getIntent().getExtras();
    String  loginName= Bundle1.getString("LoginName");
    //获取下拉菜单的值
    String style = mStyle.getSelectedItem().toString(); 
    String brand = mBrand.getSelectedItem().toString();
    String plateNumber = mPlateNumber.getSelectedItem().toString();
    String color = mColor.getSelectedItem().toString();
      EditText t1=(EditText)findViewById(R.id.et_mPlateNumber);
      String value = t1.getText().toString();       



    //  打开网络连接

    HttpClient client = new DefaultHttpClient();
    StringBuilder builder = new StringBuilder();

    // 服务器提交地址
    HttpGet myget = new HttpGet(ServerUrl
            + "OwnCar.ashx?type=addcar&LoginName=" + loginName
            +  "&PlateNumber=" + plateNumber + value
            +  "&GasOline=" + "93" 
            +  "&Brand=" + brand
            +  "&Style=" + style
            +  "&Color=" + color
            +  "&OilMass=" + "1L"
            +  "&Mileage=" + "1500"
            +  "&CarBreakDown=" + "yes" );
    try {

        HttpResponse response = client.execute(myget);
        BufferedReader reader = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        //  填充数据流
        for (String s = reader.readLine(); s != null; s = reader.readLine()) {
            builder.append(s);
        }
        // 读取json返回数组
        JSONObject jsonObject = new JSONObject(builder.toString());
        String re_result = jsonObject.getString("Result");

        if (re_result.equals("1")) {

            Toast.makeText(MycarActivity.this, "添加汽车信息成功", Toast.LENGTH_SHORT)
                    .show();

        } else {
            if (re_result.equals("0")) {

                Toast.makeText(MycarActivity.this, "已存在该汽车", Toast.LENGTH_SHORT)
                        .show();

            }else{
            Toast.makeText(MycarActivity.this, "添加汽车信息失败", Toast.LENGTH_SHORT)
                    .show();
            }
        }

        progressDialog1.dismiss();

    } catch (Exception e) {
        Log.v("url返回", "false");
        e.printStackTrace();
        Toast.makeText(MycarActivity.this, "服务器数据注册失败", Toast.LENGTH_SHORT)
                .show();
        progressDialog1.dismiss();
    }
}
  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

3条回答