title: Error when trying to call a custom popup window from another class with startActivity
body: <p>I'm new to android developing and I'm trying to open a popup window from my main class, which is created on another class.
  The problem is that I'm getting the following error when I'm trying to call the popup via a button click on main class:</p>
  
  <pre><code>android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?
  </code></pre>
  
  <p>If I move the code for popup to the button' s case, I can open by click on it, but I cannot use any of methods are on the other class(at least I don't know how).
  I've search a lot and found some similar threads, but everything I tried failed, and the error didn't change.</p>
  
  <p>So, I prefer to have all code for popup in the second class and just call it from main class, but doesn't bother me if I can have access to the methods of the second class. </p>
  
  <p>Here is the code of popup window:</p>
  
  <pre><code>@Override
  public void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      LayoutInflater inflater = (LayoutInflater) getSystemService     (Context.LAYOUT_INFLATER_SERVICE);
      //Inflate the view from a predefined XML layout
      View layout = inflater.inflate(R.layout.keyboard,
              (ViewGroup) findViewById(R.id.root));
      // create a 300px width and 470px height PopupWindow
      final PopupWindow pw = new PopupWindow(layout, 590, 400, true);
      pw.setAnimationStyle(android.R.style.Animation_Dialog);
      pw.setBackgroundDrawable(new BitmapDrawable());
      pw.setOutsideTouchable(true);
      // display the popup in the center
      pw.showAtLocation(layout, Gravity.TOP, 0, 50);
  </code></pre>
  
  <p>And the main class button's code:</p>
  
  <pre><code>case R.id.bkeyb:
          startActivity(new Intent(this, keyboard.class));
  
       break;
  </code></pre>
  
  <p>I desided to include all code for popup to the main activity and delete the second class.
  So I made a method popup, which include the above code for popup window under the onCreate of main class. I tested it with the button which I want to open the popup and worked.
  Then I made an Initialise method for all buttons of popup window, in which use findViewById for the resources and onClickListeners definition.</p>
  
  <p>The code now for popup became:</p>
  
  <pre><code>public void Popup(){
      LayoutInflater inflater = (LayoutInflater) getSystemService (Context.LAYOUT_INFLATER_SERVICE);
      //Inflate the view from a predefined XML layout
      View layout = inflater.inflate(R.layout.keyboard,
              (ViewGroup) findViewById(R.id.root));
      initialize();
      // create a 300px width and 470px height PopupWindow
      final PopupWindow pw = new PopupWindow(layout, 590, 400, true);
      pw.setAnimationStyle(android.R.style.Animation_Dialog);
      pw.setBackgroundDrawable(new BitmapDrawable());
      pw.setOutsideTouchable(true);
      // display the popup in the center
      pw.showAtLocation(layout, Gravity.TOP, 0, 50);
  
  }
  </code></pre>
  
  <p>And the onClickListener code for button which launch popup window:</p>
  
  <pre><code>    public void onClick(View v) {
      // TODO Auto-generated method stub
      switch (v.getId()){
      case R.id.bkeyb:
          Popup();
  
          switch(v.getId()) {
          case R.id.balpha:
              tvcheck.setText("Α");
             Log.e(null, "you pressed a");
          break;
          case R.id.bbeta:
              tvcheck.setText("Β");
          break;  
          }
  
       break;
  </code></pre>
  
  <p>I just added 2 buttons for testing.</p>
  
  <p>Now when press the button I get a crash with a <strong>NullPointerException.</strong>
  So again something is wrong.   </p>