Android 错误:AsyncTask 中的窗口泄漏 [英] Android Error: Window Leaked in AsyncTask

查看:17
本文介绍了Android 错误:AsyncTask 中的窗口泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是有时会出现一个错误,提示 Activity com.prueba.omnibus.EspacialTecnico 已泄露最初添加到此处的窗口 com.android.internal.policy.impl.PhoneWindow$DecorView@41e794a0当活动完成并执行 asynctask 函数时会发生这种情况.我已经搜索过了,但我不知道可能是什么问题.

I have only sometimes an error which says Activity com.prueba.omnibus.EspacialTecnico has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41e794a0 that was originally added here This happens when the activity finishes and an asynctask function is executed. I have searched but I have no idea what the problem could be.

当用户点击完成"并发生错误时执行的操作.

Action that is doen when the user clicks "finish" and the error happens.

protected Dialog onCreateDialog(int id) {
    super.onCreateDialog(id);
    switch (id) {
    case (int) DIALOG_ALERT_SALIR:
        return new AlertDialog.Builder(this)
                .setIcon(R.drawable.icon_warning)
                .setTitle(R.string.warning)
                .setMessage(R.string.confsalir)
                .setPositiveButton(R.string.alert_dialog_ok,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {
                                if (batteryReceiver == null){   
                                }
                                else{
                                    try{
                                        unregisterReceiver(batteryReceiver);
                                    }catch(IllegalArgumentException iae){
                                    }
                                    batteryReceiver = null;
                                }           



                               Correccion();
                               Parseo();
                               reloj.cancel();
                               if (Titulacion.IsReachable1(getApplicationContext())){
                                new CreateResultados().execute();


                                }
                               EspacialTecnico.this.finish();
                               try {
                                    XMLResumen.escribirXMLResume();
                                } catch (FileNotFoundException e) {
                                    // TODO Auto-generated catch block
                                    e.printStackTrace();
                                }}


                       })
                .setNegativeButton(R.string.alert_dialog_cancel,
                        new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog,
                                    int whichButton) {

                            }
                        })

                .create();
    }

    return null;

}

异步任务函数错误可能是由对话框产生的吗?

Asynctask Function Could the error be produced by the dialog?

class CreateResultados extends AsyncTask<String, String, String> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(EspacialTecnico.this);
            pDialog.setMessage("Transfiriendo...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }


        protected String doInBackground(String... args) {


            List<NameValuePair> params = new ArrayList<NameValuePair>();
            params.add(new BasicNameValuePair("id", Ident.getDNI()));
            params.add(new BasicNameValuePair("nombre", Nombres.getNombre()));
            params.add(new BasicNameValuePair("tablet", Nombres.Tablet()));
            params.add(new BasicNameValuePair("fecha", Titulacion.fecha()));
            params.add(new BasicNameValuePair("test", nombre));
            params.add(new BasicNameValuePair("correctas", correctasString));
            params.add(new BasicNameValuePair("errores", fallosString));
            params.add(new BasicNameValuePair("PC", PC));



            JSONObject json = jsonParser.makeHttpRequest(url_crear_resultados,
                    "POST", params);
            Log.d("Create Response", json.toString());


            try {
                int success = json.getInt(TAG_SUCCESS);

                if (success == 1) {


                } else {

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

            return null;
        }



        protected void onPostExecute(String file_url) {
            pDialog.dismiss();
        }
    }

有没有做过什么坏事"?感谢您的帮助

Is there anything "bad" done? Thanks for the help

推荐答案

此异常通常来自活动完成时仍处于活动状态的对话框.

This exception usually comes from dialogs that are still active when the activity is finishing.

onPreExecute() 中,您创建了一个新对话框,但可能是这样 pDialog 已经拥有对活动对话框的引用.一些解决方法:

In onPreExecute() you create a new dialog but it might be so that pDialog already holds a reference to an active dialog. Some ways around it:

  • 在创建新的之前检查 pDialog == null.解除后为 pDialog 分配一个 null.

  • Check for pDialog == null before creating a new one. Assign a null to pDialog after dismissing it.

如果 pDialog != nulldismiss() 在创建新对话框之前先将其关闭.

If pDialog != null, dismiss() it first before creating a new dialog.

(另外 super.onCreateDialog() 是不必要的,因为您没有对返回的 Dialog 做任何事情.)

(Also super.onCreateDialog() is unnecessary as you're not doing anything with the returned Dialog.)

这篇关于Android 错误:AsyncTask 中的窗口泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆