batuman
batuman

Reputation: 7304

Display ProgressDialog from AsyncTask

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) {}
     }



}

Upvotes: 0

Views: 46

Answers (2)

user3110424
user3110424

Reputation:

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.

Upvotes: 1

Sunny Garg
Sunny Garg

Reputation: 1073

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

       }

Upvotes: 1

Related Questions