Q:

ProgressDialog and AsyncTask at abnormal behavior fails

I have a AsyncTasc which turns of ProgressDialog, all works great, but after some time of abnormal behavior app fails. I know, that user will never behave so. But for good testing is necessary to try all.
Code:
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity {
    ProgressDialog mProgressDialog;
    GetSubs gs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mProgressDialog = new ProgressDialog(MainActivity.this);
        mProgressDialog.setMessage("Loading...");
        mProgressDialog.setIndeterminate(false);
        mProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mProgressDialog.setOnCancelListener(new OnCancelListener() {

            @Override
            public void onCancel(DialogInterface dialog) {
                Log.d("Called on Cancel", " Listener");
                gs.cancel(false);
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void Clicked(View view) {
        gs = new GetSubs();
        gs.execute();
    }

    public class GetSubs extends AsyncTask<Void, Void, String> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            mProgressDialog.show();
            Log.d("onPreExecute", " in AsyncTasc");
        }

        @Override
        protected String doInBackground(Void... params) {
            for (int i = 0; i < 5; i++) {
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                if (isCancelled()) break;
            }
            return null;
        }

        @Override
        protected void onCancelled() {
            // TODO Auto-generated method stub
            super.onCancelled();
            Log.d("onCancelled", " in AsyncTasc");
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            mProgressDialog.dismiss();
            Log.d("onPostExecute", " in AsyncTasc");

        }

    }

}

Logcat:
04-16 21:14:21.053: E/AndroidRuntime(28767): FATAL EXCEPTION: main
04-16 21:14:21.053: E/AndroidRuntime(28767): java.lang.IllegalArgumentException: View not attached to window manager
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:751)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:474)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.view.WindowManagerImpl$CompatModeWrapper.removeView(WindowManagerImpl.java:163)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.Dialog.dismissDialog(Dialog.java:348)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.Dialog$1.run(Dialog.java:139)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.Dialog.dismiss(Dialog.java:333)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.example.getusersubs.MainActivity$GetSubs.onPostExecute(MainActivity.java:82)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.example.getusersubs.MainActivity$GetSubs.onPostExecute(MainActivity.java:1)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.AsyncTask.finish(AsyncTask.java:602)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.AsyncTask.access$600(AsyncTask.java:156)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:615)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.os.Looper.loop(Looper.java:154)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at android.app.ActivityThread.main(ActivityThread.java:4945)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at java.lang.reflect.Method.invoke(Method.java:511)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-16 21:14:21.053: E/AndroidRuntime(28767):    at dalvik.system.NativeStart.main(Native Method)

What is wrong? Thanks for answers

A:

change:
        @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        mProgressDialog.dismiss();
        Log.d("onPostExecute", " in AsyncTasc");

    }

to 
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        if(mProgressDialog != null && mProgressDialog.isShowing()){
                mProgressDialog.dismiss();
        }
        Log.d("onPostExecute", " in AsyncTasc");

    }

The problem is that you are trying to dismiss a progress-dialog without checking if it is showing or not, the user can cancel that dialog easily by pressing backKey. 
Still you can force the user to wait for the dialog by setting Cancellable to false. that way the back-key will have no effect.

