title: How to set MediaController for a VideoView
body: <p>I am using a video view and I want to set a media controller for it. I did this code but when I click on play button the app crashes with a NullPointerException. I don't know why because I define everything truly.</p>
  <p>When I cut <code>mediaC.setAnchorView(view);</code> the video plays truly but media controller doesn't appear.</p>
  <p>Thank you for your answer.</p>
  <pre><code>public class VideoActivity extends Activity {
    Button btnPaly;
    VideoView videoPlayer;
    MediaController mediaC;
    String videoPath;
    @Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_video);
  
      btnPaly = (Button) findViewById(R.id.btnPlay);
      videoPlayer = (VideoView) findViewById(R.id.videoView);
  
      Bundle bundle = getIntent().getExtras();
      if (bundle != null) {
        videoPath = bundle.getString(Const.VIDEO_PATH);
      }
  
      btnPaly.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
  
          videoPlayer.setVideoPath(videoPath);
          videoPlayer.setMediaController(mediaC);
          mediaC.setAnchorView(view); // I get NullPointerException here
          videoPlayer.start();
        }
      });
  
    }
  }
  </code></pre>
  <p>Here is my XML:</p>
  <pre><code>&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
  &lt;android.support.constraint.ConstraintLayout
      xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
      xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
      xmlns:tools=&quot;http://schemas.android.com/tools&quot;
      android:layout_width=&quot;match_parent&quot;
      android:layout_height=&quot;match_parent&quot;
      android:id=&quot;@+id/rootLayout&quot;
      tools:context=&quot;simplevideoplayer.app.codenevisha.com.videoplayer.VideoActivity&quot;&gt;
  
  
      &lt;Button
          android:id=&quot;@+id/btnPlay&quot;
          android:layout_width=&quot;wrap_content&quot;
          android:layout_height=&quot;wrap_content&quot;
          android:layout_marginLeft=&quot;75dp&quot;
          android:text=&quot;Play&quot;
          app:layout_constraintBottom_toBottomOf=&quot;parent&quot;
          app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
          android:layout_marginStart=&quot;75dp&quot;
          android:layout_marginEnd=&quot;8dp&quot;
          android:layout_marginRight=&quot;8dp&quot;
          android:layout_marginBottom=&quot;-1dp&quot;
          app:layout_constraintRight_toRightOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.342&quot;/&gt;
  
      &lt;VideoView
          android:id=&quot;@+id/videoView&quot;
          android:layout_width=&quot;0dp&quot;
          android:layout_height=&quot;wrap_content&quot;
          app:layout_constraintTop_toTopOf=&quot;parent&quot;
          app:layout_constraintBottom_toTopOf=&quot;@+id/btnPlay&quot;
          app:layout_constraintLeft_toLeftOf=&quot;parent&quot;
          app:layout_constraintVertical_bias=&quot;1.0&quot;
          app:layout_constraintRight_toRightOf=&quot;parent&quot;
          app:layout_constraintHorizontal_bias=&quot;0.0&quot;/&gt;
  &lt;/android.support.constraint.ConstraintLayout&gt;
  </code></pre>