Android错误:AsyncTask中泄露的窗口

Kat*_*e99 2 android memory-leaks android-asynctask

我有时只会出现一个错误,说明Activity com.prueba.omnibus.EspacialTecnico has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@41e794a0 that was originally added here 当活动完成并执行asynctask函数时会发生这种情况.我搜索过,但我不知道问题是什么.

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

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;

}
Run Code Online (Sandbox Code Playgroud)

Asynctask功能可以通过对话框产生错误吗?

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();
        }
    }
Run Code Online (Sandbox Code Playgroud)

有什么"坏"吗?谢谢您的帮助

laa*_*lto 5

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

onPreExecute()您创建一个新对话框时,它可能pDialog已经保存了对活动对话框的引用.一些方法:

  • pDialog == null在创建新的之前检查.解雇后分配nullpDialog.

  • 如果pDialog != null,dismiss()它首先在创建新对话框之前.

(也没super.onCreateDialog()必要,因为你没有对返回做任何事情Dialog.)