0

I found some discussion and I followed them. But still have problem and the error at LogCat is, I think Unable to add window

04-19 16:50:47.835: E/AndroidRuntime(16147):    FATAL EXCEPTION: main
04-19 16:50:47.835: E/AndroidRuntime(16147):    android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-19 16:50:47.835: E/AndroidRuntime(16147):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:712)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:346)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:224)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at android.view.WindowManagerImpl$CompatModeWrapper.addView(WindowManagerImpl.java:149)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at android.app.Dialog.show(Dialog.java:277)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at com.test.MainActivity$2.run(MainActivity.java:213)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at android.app.Activity.runOnUiThread(Activity.java:4784)
04-19 16:50:47.835: E/AndroidRuntime(16147):    at com.test.MainActivity.displayStatus(MainActivity.java:210)

My program is

public class MainActivity extends Activity {
      protected void onCreate(Bundle savedInstanceState) {

      }
      public class SSHConnect extends AsyncTask<Integer, Integer, Integer> {    
           ProgressDialog myPd_ring;
           protected void onProgressUpdate(Integer... progress) {}

           protected void onPostExecute(Integer  result) {
              if (myPd_ring.isShowing()) {
                 myPd_ring.dismiss();;
              }
           }
           protected void onPreExecute() {
              myPd_ring= new ProgressDialog(getApplicationContext());
              myPd_ring.setCancelable(true);
              myPd_ring.setTitle("Please wait!");
              myPd_ring.setMessage("Connecting...");
              myPd_ring.setIndeterminate(true);
              myPd_ring.setProgressStyle(ProgressDialog.STYLE_SPINNER);
              myPd_ring.show();

           }
           @Override
           protected Integer doInBackground(Integer... params) {}
     }



}
Community
  • 1
  • 1
batuman
  • 7,066
  • 26
  • 107
  • 229
  • 1
    use `myPd_ring= new ProgressDialog(MainActivity.this);` in place of `myPd_ring= new ProgressDialog(getApplicationContext());` –  Apr 19 '14 at 09:17
  • I have answered so you can accept and i have also given a link plz go through this, this will elaborate why we shouldn't use getApplicationContext() –  Apr 19 '14 at 09:24

2 Answers2

1

Try to use.

protected void onPreExecute() {
          myPd_ring= new ProgressDialog(MainActivity.this);
          myPd_ring.setCancelable(true);
          myPd_ring.setTitle("Please wait!");
          myPd_ring.setMessage("Connecting...");
          myPd_ring.setIndeterminate(true);
          myPd_ring.setProgressStyle(ProgressDialog.STYLE_SPINNER);
          myPd_ring.show();

       }
Sunny Garg
  • 1,073
  • 1
  • 6
  • 13
1

Use

myPd_ring= new ProgressDialog(MainActivity.this);

In place of

myPd_ring= new ProgressDialog(getApplicationContext());

getApplicationContext in AsyncTask class?

Extracted from the above link.

" Why does getApplicationContext() not work from the UI portions of AsyncTask ?" -- because getApplicationContext() is a method on Context, and AsyncTask does not inherit from Context.

Community
  • 1
  • 1