0

shows error in dialog.dismiss() which is called in onPostExecute().dialog.show() is called in onPreExecute()

this task was called to a login activity.

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..
    }

I haven't face the exception before so details would be nice.

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)

Activity class

Signin button click portion

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();




        }
    });
Fay007
  • 2,100
  • 2
  • 23
  • 51

4 Answers4

0

Try doing this.. (just surround them by this code)

YourActivity.this.runOnUiThread(new Runnable() {
    public void run() {
        //TODO add your show() and dismiss() here
    }
});
Galax
  • 359
  • 1
  • 13
0

I do not think you can interact with the layout in the doInBackground method.

Try replacing: Utility.showMessage(appContext, "Cannot Connect To Internet");

with a log or if you really want the user to know, then use the onPostExecute for it.

Alexandra Alstanei
  • 491
  • 1
  • 4
  • 5
  • Ok, then try the solution here: http://stackoverflow.com/questions/6614692/progressdialog-how-to-prevent-leaked-window You are also parsing an activity. Try parsing a context instead. – Alexandra Alstanei Nov 13 '14 at 15:17
0

It must not showing error actually, Even though in such cases you have to verify the dialog object is not null and also isShowing. like the below,

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

dialog.dismiss(); }

It will used to avoid such kind of exception.

Srinivasan
  • 3,863
  • 3
  • 22
  • 33
0

The most common reason for this error message is that the Activity to which the dialog is shown over has been closed prior to dismissing the dialog - causing leaked window.

In your scenario, as you are introducing AsyncTask, there might be a possibility that the activity which executed the task has been terminated prematurely. For example: a back-key is pressed by user, no-history attribute in manifest for that activity or due to some other reasons.

There might also be a possibility that you are finishing the activity right-after executing the async task. In that case, the dialog also cannot survive as the parent window is terminated.

For a safeguard, I would suggest you to include dialog.setCancelable(false); to your dialog. This will block the user from finishing the activity. Once the dialog is dismissed after reaching onPostExecute, user is allowed to finish activity. Or make sure you are not finishing the activity async task reached onPostExecute.

Or an alternate would be to take the dialog out from AsyncTask and attach it to Activity's life cycle where you may simply dismiss the dialog in onDestroy on your activity.

waqaslam
  • 64,866
  • 15
  • 157
  • 170
  • There's quite much assumption in my answer, but it will always be helpful if you attach full code (together with activity) for direct answers :) – waqaslam Nov 13 '14 at 15:32
  • yes i am finishing the activity after executing the async task – Fay007 Nov 13 '14 at 15:51
  • So that is actually the problem. You cannot gracefully finish the activity without dismissing all the dialogs attached to it. Review your design approach to avoid finishing the activity which hosts dialog and you should be good to go. – waqaslam Nov 13 '14 at 15:54
  • i have added the activity class in the question – Fay007 Nov 13 '14 at 15:55
  • There's nothing much I can recommend at this point as it totally depends on your design approach. However, you should not finish **SignInActivity** while **AsyncTask** is working. Perhaps wait till the result is returned from async and then depending on the success or failure, launch the corresponding activity or show error message. – waqaslam Nov 13 '14 at 16:01