2 sinat 34047321 sinat_34047321 于 2016.04.22 19:33 提问

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

安卓中 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个回答

Wei_Smile
Wei_Smile   2016.04.22 19:42

这个意思是你这个Handler类最好是声明为static的要不然可能会产生内存泄漏,解决的办法是在你这个Handler类前面加一个static参数,看会不会报错了
也就是public static class XXXX

zhuod
zhuod   2016.04.22 22:34

你在handler定义的前面加一个final进行修饰一下,因为击事件中的引用变量需要是final类型的,除非是这个点击事件方法中的局布变量不用声明为final

dongfeng9ge
dongfeng9ge   Ds   Rxr 2016.04.30 16:17

在这个handler前面加上static

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!