title: Android: ProgressDialog.show() WindowManager token, not valid
body: <pre><code>public class MainActivity extends Activity {
  
      private static String State = "STOP";
  
      final String address = "http://XXXX.co.uk:9994";
      private ImageView Player;
      private ProgressDialog progress;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
          // requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
          setContentView(R.layout.activity_main);
  
  
          Button Schedule;
          Button Presenters;
          Button Image;
  
          Player = (ImageView) findViewById(R.id.imgStart);
  
          if(State != "STOP"){
              Player.setImageResource(R.drawable.stop);       
          }
          else{
              Player.setImageResource(R.drawable.start);
          }
  
          Schedule = (Button) findViewById(R.id.btnSchedule);
          Presenters = (Button) findViewById(R.id.btnPresenters);
          Image = (Button) findViewById(R.id.imgbutton);
          LocalBroadcastManager.getInstance(this).registerReceiver(LoadingReceiver, new IntentFilter("loading"));
          LocalBroadcastManager.getInstance(this).registerReceiver(CompletionReceiver, new IntentFilter("done"));
          LocalBroadcastManager.getInstance(this).registerReceiver(ErrorReceiver, new IntentFilter("error"));
  
  
  
          Player.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  if(State != "PLAY"){
                      Intent intent = new Intent(MainActivity.this,
                              RadioPlayer.class);
                      intent.putExtra(RadioPlayer.START_PLAY, true);
  
                      startService(intent);
                      Player.setImageResource(R.drawable.stop);
                      State = "PLAY";
                  }
                  else{
                      Intent intent = new Intent(MainActivity.this,
                              RadioPlayer.class);
                      stopService(intent);
                      Player.setImageResource(R.drawable.start);
  
                      State = "STOP";
                  }
              }
  
          });
  
          Schedule.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
  
                  Intent scheduleListing = new Intent(MainActivity.this,
                          Schedule.class);
                  startActivity(scheduleListing);
              }
          });
  
          Presenters.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View v) {
                  Intent PresenterList = new Intent(MainActivity.this,
                          Presenters.class);
                  startActivity(PresenterList);
              }
          });
  
          Image.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  Intent ImgGet = new Intent(MainActivity.this,
                          OnAir.class);
                  startActivity(ImgGet);
  
  
              }
          }); 
  
      }// End On Create
  
  
      BroadcastReceiver LoadingReceiver = new BroadcastReceiver(){
  
          @SuppressLint("NewApi")
          @Override
          public void onReceive(Context arg0, Intent arg1) {
              progress = new ProgressDialog(MainActivity.this);
              progress.setTitle("Loading");
              progress.setMessage("Loading RadioPlymouth...");
              progress.show(); //error here
              Player.setImageResource(R.drawable.stop);
          }
      };
  
      BroadcastReceiver CompletionReceiver = new BroadcastReceiver(){
  
          @Override
          public void onReceive(Context context, Intent intent) {
              progress.dismiss();
              Player.setImageResource(R.drawable.stop);
  
          }
  
      };
  
      BroadcastReceiver ErrorReceiver = new BroadcastReceiver(){
  
          @Override
          public void onReceive(Context context, Intent intent) {
              progress.dismiss();
              State = "STOP";
              Player.setImageResource(R.drawable.start);
  
          }
  
      };
  
  
  }
  </code></pre>
  
  <p><br></p>
  
  <pre><code>06-28 14:33:14.988: E/AndroidRuntime(18960): android.view.WindowManager$BadTokenException: Unable to add window -- token android.os.BinderProxy@41888998 is not valid; is your activity running?
  06-28 14:33:14.988: E/AndroidRuntime(18960):    at android.view.ViewRootImpl.setView(ViewRootImpl.java:564)
  06-28 14:33:14.988: E/AndroidRuntime(18960):    at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:269)
  06-28 14:33:14.988: E/AndroidRuntime(18960):    at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
  06-28 14:33:14.988: E/AndroidRuntime(18960):    at android.app.Dialog.show(Dialog.java:289)
  06-28 14:33:14.988: E/AndroidRuntime(18960):    at com.radioplymouth.RadioPlymouth.MainActivity$1.onReceive(MainActivity.java:144)
  </code></pre>
  
  <p>The error occurs when i change activitys (Example), From MainActivity -> goto Schedule page -> return to MainActivity. someone has previously suggested that i need to handle the window token in onPause() and/or onResume() however my attempts todo so have not been sucsessful. without changing activitys, the code runs fine. if anyone can spot what i am missing, that'll be great :)</p>