在dialog.dismiss()中显示android.view.windowleaked错误

时间:2014-11-13 14:59:39

标签: android

  
    

显示在onPostExecute()中调用的dialog.dismiss()中的错误。在onPreExecute()中调用dialog.show()

  

此任务被调用到登录活动。

public class SignInTask extends AsyncTask<String, String, String>{

    ProgressDialog dialog;
    String responseString;

    SignInActivity appContext;
    String url;

    String pageUrl="signin?";
    String emailParameter="email=";
    String passwordParameter="pass=";
    String user_device="android";

    //String userName,passWord;
    SignInModel signInModel;


    public SignInTask(SignInActivity c,SignInModel model)
    {
        appContext=c;
        signInModel = model;

        url=Utility.baseUrl+pageUrl+emailParameter+signInModel.getEmail()+"&"+passwordParameter+signInModel.getPassword()+"&"+user_device;
        //url="http://bioscopebd.com/mobileappand/signin";

        Log.i("url", url);
    }

    protected void onPreExecute(){

        dialog = new ProgressDialog(appContext);
        dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER); 
        dialog.setMessage("Login...");
        dialog.show();

        super.onPreExecute();

    }

    String filterResponseString(String r)
    {
        return r.replace("\r\n", "");
    }

    @Override
    protected String doInBackground(String... uri) {

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;

        try {

            response = httpclient.execute(new HttpGet(url));
            StatusLine statusLine = response.getStatusLine();

            if(statusLine.getStatusCode() == HttpStatus.SC_OK){

                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
                responseString=filterResponseString(responseString);

            } 
            else
            {
                //Closes the connection.
               response.getEntity().getContent().close();

               Utility.showMessage(appContext, "Cannot Connect To Internet");
            }
        }  
        catch (Exception e) 
        {
            //TODO Handle problems..
        }
        return responseString;
    }





    @Override
    protected void onPostExecute(String result) 
    {
        dialog.dismiss();

        if(responseString!=null)
        {
            Log.i("response String", responseString);
            //appContext.signInDataLoaded(responseString);


            JSONObject jObj;
            //SignInModel signIn = new SignInModel();

            try {

                jObj = new JSONObject(responseString);


                signInModel.setStatus( jObj.getBoolean("status") );
                signInModel.setUserId( jObj.getString("user_id") );

                appContext.signInDataLoaded(signInModel);



            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }




        }else
        {
            Log.i("response String", "NULL");
        }

        super.onPostExecute(result);
        //Do anything with response..
    }

我之前没有面对异常,所以细节会很好。

logcat的

11-13 23:27:39.670: E/WindowManager(826): android.view.WindowLeaked: Activity com.bioscope.main.SignInActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{b2a14910 V.E..... R......D 0,0-684,192} that was originally added here
11-13 23:27:39.670: E/WindowManager(826):   at android.view.ViewRootImpl.<init>(ViewRootImpl.java:348)
11-13 23:27:39.670: E/WindowManager(826):   at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
11-13 23:27:39.670: E/WindowManager(826):   at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
11-13 23:27:39.670: E/WindowManager(826):   at android.app.Dialog.show(Dialog.java:286)
11-13 23:27:39.670: E/WindowManager(826):   at com.bioscope.server.SignInTask.onPreExecute(SignInTask.java:55)
11-13 23:27:39.670: E/WindowManager(826):   at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
11-13 23:27:39.670: E/WindowManager(826):   at android.os.AsyncTask.execute(AsyncTask.java:535)
11-13 23:27:39.670: E/WindowManager(826):   at com.bioscope.main.SignInActivity$2.onClick(SignInActivity.java:91)
11-13 23:27:39.670: E/WindowManager(826):   at android.view.View.performClick(View.java:4438)
11-13 23:27:39.670: E/WindowManager(826):   at android.view.View$PerformClick.run(View.java:18422)
11-13 23:27:39.670: E/WindowManager(826):   at android.os.Handler.handleCallback(Handler.java:733)
11-13 23:27:39.670: E/WindowManager(826):   at android.os.Handler.dispatchMessage(Handler.java:95)
11-13 23:27:39.670: E/WindowManager(826):   at android.os.Looper.loop(Looper.java:136)
11-13 23:27:39.670: E/WindowManager(826):   at android.app.ActivityThread.main(ActivityThread.java:5017)
11-13 23:27:39.670: E/WindowManager(826):   at java.lang.reflect.Method.invokeNative(Native Method)
11-13 23:27:39.670: E/WindowManager(826):   at java.lang.reflect.Method.invoke(Method.java:515)
11-13 23:27:39.670: E/WindowManager(826):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-13 23:27:39.670: E/WindowManager(826):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-13 23:27:39.670: E/WindowManager(826):   at dalvik.system.NativeStart.main(Native Method)
11-13 23:27:41.190: D/AndroidRuntime(826): Shutting down VM
11-13 23:27:41.190: W/dalvikvm(826): threadid=1: thread exiting with uncaught exception (group=0xb1a24ba8)
11-13 23:27:41.210: E/AndroidRuntime(826): FATAL EXCEPTION: main
11-13 23:27:41.210: E/AndroidRuntime(826): Process: com.bioscope, PID: 826
11-13 23:27:41.210: E/AndroidRuntime(826): java.lang.IllegalArgumentException: View=com.android.internal.policy.impl.PhoneWindow$DecorView{b2a14910 V.E..... R......D 0,0-684,192} not attached to window manager
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.view.WindowManagerGlobal.findViewLocked(WindowManagerGlobal.java:370)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.view.WindowManagerGlobal.removeView(WindowManagerGlobal.java:299)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.view.WindowManagerImpl.removeViewImmediate(WindowManagerImpl.java:84)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.app.Dialog.dismissDialog(Dialog.java:329)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.app.Dialog.dismiss(Dialog.java:312)
11-13 23:27:41.210: E/AndroidRuntime(826):  at com.bioscope.server.SignInTask.onPostExecute(SignInTask.java:108)
11-13 23:27:41.210: E/AndroidRuntime(826):  at com.bioscope.server.SignInTask.onPostExecute(SignInTask.java:1)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.os.AsyncTask.finish(AsyncTask.java:632)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.os.AsyncTask.access$600(AsyncTask.java:177)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.os.Handler.dispatchMessage(Handler.java:102)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.os.Looper.loop(Looper.java:136)
11-13 23:27:41.210: E/AndroidRuntime(826):  at android.app.ActivityThread.main(ActivityThread.java:5017)
11-13 23:27:41.210: E/AndroidRuntime(826):  at java.lang.reflect.Method.invokeNative(Native Method)
11-13 23:27:41.210: E/AndroidRuntime(826):  at java.lang.reflect.Method.invoke(Method.java:515)
11-13 23:27:41.210: E/AndroidRuntime(826):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
11-13 23:27:41.210: E/AndroidRuntime(826):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
11-13 23:27:41.210: E/AndroidRuntime(826):  at dalvik.system.NativeStart.main(Native Method)

活动类

登录按钮点击部分

signIn.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {

            SignInModel signInModel = new SignInModel();

            String email = emailEditText.getText().toString().trim();
            String password = passwordEditText.getText().toString().trim(); 


            if(FormHelper.isValidEmail(email) )

            signInModel.setEmail(email);
            signInModel.setPassword(password);

            //Utility.showMessage(SignInActivity.this, signInModel.getEmail()+" "+signInModel.getPassword());

            new SignInTask(SignInActivity.this,signInModel).execute();




            Intent i=new Intent(SignInActivity.this, HomeActivity.class);
            i.putExtra("user_check",1 );
            startActivity(i);
            finish();




        }
    });

4 个答案:

答案 0 :(得分:0)

尝试这样做..(只需用这段代码包围它们)

YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        //TODO add your show() and dismiss() here
    }
});

答案 1 :(得分:0)

我认为你不能在doInBackground方法中与布局进行交互。

尝试替换:  Utility.showMessage(appContext,&#34;无法连接到Internet&#34;);

使用日志或者如果您真的希望用户知道,那么请使用onPostExecute。

答案 2 :(得分:0)

实际上不能显示错误, 即使在这种情况下,您还必须验证对话框对象不为null并且还要显示。如下所示,

if(dialog != null && dialog.isShowing()){

dialog.dismiss(); }

它将用于避免这种异常。

答案 3 :(得分:0)

此错误消息的最常见原因是在关闭对话框之前已关闭显示对话框的Activity - 导致泄露的窗口。

在您的方案中,当您介绍AsyncTask时,执行任务的活动可能会过早终止。例如:用户按下后退键,该活动的清单中的 no-history 属性或由于某些其他原因。

执行异步任务后,您可能还有可能正在完成活动。在这种情况下,当父窗口终止时,对话框也无法生存。

为了保障,我建议您在对话框中加入dialog.setCancelable(false);。这将阻止用户完成活动。在到达 onPostExecute 后关闭对话框后,允许用户完成活动。或者确保您没有完成活动异步任务到达 onPostExecute

或者替代方法是从AsyncTask中取出对话框并将其附加到Activity的生命周期中,您可以简单地忽略活动中 onDestroy 中的对话框。