Activity has leaked window/ BadToken Exception?
<p>In my application, I am using dropbox library and the probelm comes when first time I will click on dropbox file download, it start downloading file and save that file into my sdcard...</p>

<p>But when second time when I click on dropbox file download button, first it authenticate then after authentication, It is showing me the following errors???????</p>

<blockquote>
  <p>03-28 16:17:32.610: E/WindowManager(20903): Activity
  xyz.dropboxActivity has leaked window
  com.android.internal.policy.impl.PhoneWindow$DecorView@2f474670 that
  was originally added here 03-28 16:17:32.610: E/WindowManager(20903):
  android.view.WindowLeaked: Activity xyz.dropboxActivity has leaked
  window com.android.internal.policy.impl.PhoneWindow$DecorView@2f474670
  that was originally added here 03-28 16:17:32.660:
  E/AndroidRuntime(20903): android.view.WindowManager$BadTokenException:
  Unable to add window -- token android.os.BinderProxy@2f4d63e0 is not
  valid; is your activity running?</p>
</blockquote>

<p>CAN SOMEONE PLS SUGGEST ME WHYY???</p>

<p><strong>[Edit]</strong></p>

<pre><code>    final static private String APP_KEY = "";
    final static private String APP_SECRET = "";
    private static final String TAG = "Dropbox";

    final static private AccessType ACCESS_TYPE = AccessType.DROPBOX;
    final static private String ACCOUNT_PREFS_NAME = "prefs";
    final static private String ACCESS_KEY_NAME = "ACCESS_KEY";
    final static private String ACCESS_SECRET_NAME = "ACCESS_SECRET";
    DropboxAPI&lt;AndroidAuthSession&gt; mApi;
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    AndroidAuthSession session = buildSession();
    mApi = new DropboxAPI&lt;AndroidAuthSession&gt;(session);

      checkAppKeySetup();

        // Start the remote authentication

        mApi.getSession().startAuthentication(dropboxActivity.this);

        Log.i("mApi.getSessionm", ""+mApi.getSession());
        }

     protected void onResume() {
        super.onResume();
        AndroidAuthSession session = mApi.getSession();

        // The next part must be inserted in the onResume() method of the
         // activity from which session.startAuthentication() was called, so
        // that Dropbox authentication completes properly.
         if (session.authenticationSuccessful()) {
            try {

              // Mandatory call to complete the auth
              session.finishAuthentication();
               Log.i("4", "4");


            /*Intent i=getIntent();
            Bundle b=i.getExtras();
            String FileName=b.getString("Filename");*/
          String FileName="sample_1.csv";
            Log.i("FileName",FileName);

          download download_obj=new download(dropboxActivity.this, mApi,FileName); //The error comes at this line when I am calling download with this context
          download_obj.execute();
         } catch (IllegalStateException e) {
            showToast("Couldn't authenticate with Dropbox:" + e.getLocalizedMessage());
            Log.i(TAG, "Error authenticating", e);
        }
</code></pre>

<p>A*<em>nd the download.java file is:</em>*</p>

<pre><code>    public class download extends AsyncTask&lt;Void,Integer,Boolean&gt;{
private Activity context;
ProgressDialog dialog=null;
DropboxAPI&lt;AndroidAuthSession&gt; mApi;
String FileName;
download(Activity context,DropboxAPI&lt;AndroidAuthSession&gt; mApi,String FileName)
{
    this.context=context;
    this.mApi=mApi;
    this.FileName=FileName;
}
 @Override
protected void onPreExecute()
    {
     dialog=new ProgressDialog(context);
     dialog.setMessage("Loading...");
     dialog.show();
    }

     @Override
      protected Boolean doInBackground(Void... arg0) {
    // TODO Auto-generated method stub
      File file= new File("/sdcard/"+FileName);
              OutputStream out= null;
              boolean result=false;
               try {
            out = new BufferedOutputStream(new FileOutputStream(file));
        } catch (FileNotFoundException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        try {
               DropboxFileInfo info = mApi.getFile("aaa"+FileName, null, out, null);
               Log.i("DbExampleLog", "The file's rev is: " + info.getMetadata().rev);
               Intent JumpToParseCSV=new Intent(context,ParseCSV.class);
                JumpToParseCSV.putExtra("FileName", file.getAbsolutePath());
                Log.i("path", "FileName"+ file.getAbsolutePath());
                 ((Activity) context).finish();
                context.startActivity(JumpToParseCSV);
                result=true;
            } catch (DropboxException e) {
               Log.e("DbExampleLog", "Something went wrong while downloading.");
               file.delete();
               result=false;
            }


    return result;
}
 @Override
 protected void onProgressUpdate(Integer... values)
 {
     super.onProgressUpdate(values);
 // dialog.incrementProgressBy(5);   
}
@Override
protected void onPostExecute(Boolean result)
{
    dialog.dismiss();

    if(result==true)
    {

        new AlertDialog.Builder(context)


        .setTitle("Info")

        .setMessage("Selected file has been uploaded, Please navigate to DashBoard!!")
        .setPositiveButton("OK", 

                new DialogInterface.OnClickListener() {



            @Override

            public void onClick(DialogInterface dialog, int which) {

                // TODO Auto-generated method stub

            }

        }).show();


    }
    else
    {
        new AlertDialog.Builder(context)


        .setTitle("Info")

        .setMessage("Something went wrong while downloading!!")
        .setPositiveButton("OK", 

                new DialogInterface.OnClickListener() {



            @Override

            public void onClick(DialogInterface dialog, int which) {

                // TODO Auto-generated method stub
                Intent i=new Intent(context,jjj.class);
                ((Activity) context).finish();
                context.startActivity(i);
            }

        }).show();
    }
        //Log.i("database", "completed database");

}
</code></pre>

<p>}</p>

<p><strong>[EDIT]</strong></p>

<pre><code>Activity xyz.dropboxActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405f4d20 that was originally added here
android.view.WindowLeaked: Activity com.kxs.allen.dropboxActivity has leaked window 
com.android.internal.policy.impl.PhoneWindow$DecorView@405f4d20 that was originally added here
    at android.view.ViewRoot.&lt;init&gt;(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:424)
    at android.app.Dialog.show(Dialog.java:241)
    at com.kxs.allen.download.onPreExecute(download.java:39)
    at android.os.AsyncTask.execute(AsyncTask.java:391)
    at com.kxs.allen.dropboxActivity.onResume(dropboxActivity.java:139)
    at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
    at android.app.Activity.performResume(Activity.java:3832)
    at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
    at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3733)
    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:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
    at dalvik.system.NativeStart.main(Native Method)
threadid=1: thread exiting with uncaught exception (group=0x40018560)
FATAL EXCEPTION: main java.lang.IllegalArgumentException: View not attached to window manager
    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
    at android.app.Dialog.dismissDialog(Dialog.java:278)
    at android.app.Dialog.access$000(Dialog.java:71)
    at android.app.Dialog$1.run(Dialog.java:111)
    at android.app.Dialog.dismiss(Dialog.java:268)
    at com.kxs.allen.download.onPostExecute(download.java:81)
    at com.kxs.allen.download.onPostExecute(download.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:417)
    at android.os.AsyncTask.access$300(AsyncTask.java:127)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3733)
    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:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
    at dalvik.system.NativeStart.main(Native Method)
03-28 17:47:37.199: E/AndroidRuntime(2489): [Blue Error Handler] Make Debugging Report file for main
03-28 17:47:37.199: E/AndroidRuntime(2489): **java.lang.IllegalArgumentException: View not attached to window manager**
    at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
    at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
    at android.view.Window$LocalWindowManager.removeView(Window.java:432)
    at android.app.Dialog.dismissDialog(Dialog.java:278)
    at android.app.Dialog.access$000(Dialog.java:71)
    at android.app.Dialog$1.run(Dialog.java:111)
    at android.app.Dialog.dismiss(Dialog.java:268)
    at com.kxs.allen.download.onPostExecute(download.java:81)
    at com.kxs.allen.download.onPostExecute(download.java:1)
    at android.os.AsyncTask.finish(AsyncTask.java:417)
    at android.os.AsyncTask.access$300(AsyncTask.java:127)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3733)
    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:893)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:651)
    at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>Please suggesdt me something???</p>
