我正在使用視頻視圖,我想爲它設置一個媒體控制器,並且我像下面的代碼那樣做了它,但是當我點擊播放按鈕時,應用程序將會與null點異常一起崩潰。我不知道爲什麼,因爲我確定了一切。 (當我削減'mediaC.setAnchorView(查看);'視頻將真正播放,但媒體控制器不會出現。) 謝謝你的回答。如何爲videoview設置mediacontroller
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 Null piont exception here
videoPlayer.start();
}
});
}
}
這裏是我的XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootLayout"
tools:context="simplevideoplayer.app.codenevisha.com.videoplayer.VideoActivity">
<Button
android:id="@+id/btnPlay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="75dp"
android:text="Play"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="75dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="-1dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.342"/>
<VideoView
android:id="@+id/videoView"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/btnPlay"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintVertical_bias="1.0"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.0"/>
</android.support.constraint.ConstraintLayout>
就在上定義的MediaController對象創建,然後將視圖添加到否則錯誤會喜歡在空對象引用中添加視圖...... –