Logcat显示WindowLeaked

时间:2015-06-12 08:00:16

标签: android android-activity android-logcat

即使我的应用中没有崩溃。但是logcat显示了这个调试日志。我想知道导致这个问题的原因。我对线程的了解不多,所以这可能与糟糕的线程使用有关。

06-12 12:37:37.349  12433-12433/com.ets.medecord E/WindowManager﹕ Activity com.ets.medecord.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405403d0 that was originally added here
    android.view.WindowLeaked: Activity com.ets.medecord.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405403d0 that was originally added here
            at android.view.ViewRoot.<init>(ViewRoot.java:259)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
            at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
            at android.view.Window$LocalWindowManager.addView(Window.java:465)
            at android.app.Dialog.show(Dialog.java:241)
            at com.ets.medecord.LoginActivity.attemptLogin(LoginActivity.java:125)
            at com.ets.medecord.LoginActivity.access$300(LoginActivity.java:45)
            at com.ets.medecord.LoginActivity$1.onClick(LoginActivity.java:87)
            at android.view.View.performClick(View.java:2506)
            at android.view.View$PerformClick.run(View.java:9112)
            at android.os.Handler.handleCallback(Handler.java:587)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:130)
            at android.app.ActivityThread.main(ActivityThread.java:3835)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:507)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
            at dalvik.system.NativeStart.main(Native Method)

这是我正在使用的代码,看看是否可以。 在这里,我尝试在工作线程完成它的工作后解除对话。

final String user_email = email;
final String user_password = password;
Log.d(TAG, "email:" + email);
Log.d(TAG, "password:" + password);
pDialog = new ProgressDialog(this);
pDialog.setMessage("Loading....");
pDialog.show();
new Thread(new Runnable() {
    @Override
    public void run() {
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse httpResponse = null;
        HttpPost httpPost = new HttpPost("http://elitetotality.com/medicord/");
        List<NameValuePair> nameValuePair = new ArrayList<>();
        nameValuePair.add(new BasicNameValuePair("tag", "login"));
        nameValuePair.add(new BasicNameValuePair("email", user_email));
        nameValuePair.add(new BasicNameValuePair("password", user_password));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        try {
            httpResponse = httpClient.execute(httpPost);
        } catch (IOException e) {
            e.printStackTrace();
        }


        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    httpResponse.getEntity().getContent(), "UTF-8"));
            StringBuilder builder = new StringBuilder();

            for (String line = null; (line = reader.readLine()) != null; ) {
                builder.append(line).append("\n");
            }

            String httpResponseString = builder.toString();
            strResponse = httpResponseString;
            Log.d(TAG, httpResponseString);

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

        handler.post(new Runnable() {
            @Override
            public void run() {
                pDialog.hide();
                pDialog.dismiss();
            }
        });
        JSONObject reader;
        try {
            if(strResponse != null){
                reader = new JSONObject(strResponse);
                String error = reader.get("error").toString();
                Log.d(JSON_TAG, error);
                if(error.equals("false")) {
                    String userName = reader.getJSONObject("user").get("name").toString();
                    editor.putBoolean("isLoggedIn", true);
                    editor.putString("userName", userName);
                    editor.apply();
                    startActivity(new Intent(getApplicationContext(), RandomProfileActivity.class));
                    finish();
                } else {

                    handler.post(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),"Either email or password is incorrect!"
                                    ,Toast.LENGTH_SHORT).show();
                        }
                    });
                }


            }

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


    }
}).start();

2 个答案:

答案 0 :(得分:1)

更详细的答案

更改此部分代码:

    handler.post(new Runnable() {
        @Override
        public void run() {
            pDialog.hide();
            pDialog.dismiss();
        }
    });

为:

pDialog.dismiss();

Handler.post将可运行代码置于当前方法完成后的某个时间运行。这就是你泄漏警告的原因,你正在泄漏一个对话框。

Android提供AsyncTask来进行短期异步操作。您可以使用onPreExecute来展示您的DialogonPostExecute来解散它。

答案 1 :(得分:0)

在您退出Dialog后,您正试图解雇Activity。由于您的工作线程将在您的活动被销毁后继续执行。如果您在Activity被销毁后尝试关闭对话框,则会导致leaked window exception

尝试将此代码添加到您的活动中。

@Override
public void onDestroy() {
    super.onDestroy();
    if (dialog != null) {
        dialog.dismiss();
        dialog = null;
    }
}