title: How to use MediaController.MediaPlayerControl?
body: <p>I am trying to use <code>MediaController.MediaPlayerControl</code> in order to display a <code>MediaController</code> at the bottom of my Custom View but I can't get it to work. It's crashing at the <code>ctrl.show();</code> with the following Exception whenever I try to load this view:</p>
  
  <blockquote>
    <p>E/AndroidRuntime( 3007): Caused by: android.view.WindowManager$BadTokenException: Unable to add window -- token null is not valid; is your activity running?</p>
  </blockquote>
  
  <p>here is my code.</p>
  
  <pre><code>public class MediaPlayerView extends ImageView implements MediaPlayerControl {
  
      private MediaPlayer mp;
      private MediaController ctrl;
      private Uri data;
      private Context mContext;
  
      public MediaPlayerView(Context context, Uri data) {
          super(context);
          this.mContext = context;
          this.data = data;
          init();
      }
  
      public void init() {
          mp = new MediaPlayer();
          try {
              mp.setDataSource(mContext, data);
              mp.prepare();
          } catch(IOException e) {
              e.printStackTrace();
          }
          ctrl = new MediaController(mContext);
          ctrl.setMediaPlayer(this);
          ctrl.setAnchorView(this);
          ctrl.setEnabled(true);
          ctrl.show();
      }
  
      public boolean canPause() {
          return true;
      }
  
      public boolean canSeekBackward() {
          return false;
      }
  
      public boolean canSeekForward() {
          return false;
      }
  
      public int getBufferPercentage() {
          return (mp.getCurrentPosition()*100)/mp.getDuration();
      }
  
      public int getCurrentPosition() {
          return mp.getCurrentPosition();
      }
  
      public int getDuration() {
          return mp.getDuration();
      }
  
      public boolean isPlaying() {
          return mp.isPlaying();
      }
  
      public void pause() {
          mp.pause();
      }
  
      public void seekTo(int pos) {
          mp.seekTo(pos);
      }
  
      public void start() {
          mp.start();
      }
  }
  </code></pre>
  
  <p>Any help greatly appreciated!</p>