Android - mediacontroller.show() 处的空指针异常
Android - Null Pointer Exception at mediacontroller.show()
我正在开发一个 android 应用程序,我试图在其中播放来自 URL 的视频。
下面提到xml中的视频视图,
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
在 ViewPager 内的片段中,来自 xml 的 VideoView 引用被调用并附加到 MediaController
videoView = (VideoView) studyVideo.findViewById(R.id.VideoView);
mediaController = new MediaController(getActivity());
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(studyVideoUrl));
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
isVideoBuffered=true;
videoView.start();
}
});
我可以显示如下视频,
在这个截图中,MediaController
显示在Activity
的底部。但是,我需要像在Youtube中那样显示在VideoView的底部。
请帮我找到解决办法。
更新:
我已经在 xml 中添加了 MediaController,如下所示,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_above="@+id/mediaController"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<MediaController
android:id="@+id/mediaController"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</MediaController>
</RelativeLayout>
在Java,我更新了两个地方,
已替换
mediaController = new MediaController(getActivity());
与
(MediaController) studyVideo
.findViewById(R.id.mediaController);
在 onPrepared(MediaPlayer mp)
方法中添加了 mediaController.show();
但是,应用程序崩溃并出现以下 logcat 错误,
07-06 00:25:25.936: E/MediaPlayer(32614): setDataSource: IOException! uri=https://ia700401.us.archive.org/19/items/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4
07-06 00:25:35.331: E/AndroidRuntime(32614): FATAL EXCEPTION: main
07-06 00:25:35.331: E/AndroidRuntime(32614): java.lang.NullPointerException
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.widget.MediaController.show(MediaController.java:435)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.widget.MediaController.show(MediaController.java:394)
07-06 00:25:35.331: E/AndroidRuntime(32614): at com.prime.soft.fragments.StudyVideoFragment.onPrepared(StudyVideoFragment.java:70)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.widget.VideoView.onPrepared(VideoView.java:399)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2125)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.os.Handler.dispatchMessage(Handler.java:107)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.os.Looper.loop(Looper.java:194)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.app.ActivityThread.main(ActivityThread.java:5391)
07-06 00:25:35.331: E/AndroidRuntime(32614): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 00:25:35.331: E/AndroidRuntime(32614): at java.lang.reflect.Method.invoke(Method.java:525)
07-06 00:25:35.331: E/AndroidRuntime(32614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-06 00:25:35.331: E/AndroidRuntime(32614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-06 00:25:35.331: E/AndroidRuntime(32614): at dalvik.system.NativeStart.main(Native Method)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<MediaController
android:id="@+id/mediaController"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</MediaController>
<VideoView
android:id="@+id/VideoView"
android:layout_above="@+id/mediaController"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
而且应该是这个
mediaController = (MediaController)
.findViewById(R.id.mediaController);
而不是这个,
(MediaController) studyVideo
.findViewById(R.id.mediaController);
或者您是否碰巧更改了 mediaPlayer member 变量的名称?
最后我通过扩展 MediaController
解决了
更新代码:
videoView = (VideoView) studyVideo.findViewById(R.id.VideoView);
mediaController = new ConstantAnchorMediaController(getActivity(),
videoView);
try {
mediaController.setAnchorView(videoView);
mediaController.setMediaPlayer(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(studyVideoUrl));
} catch (Exception e) {
AlertMessage.showAlertMessage(getActivity(), e.toString());
}
videoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
isVideoBuffered = true;
videoView.requestFocus();
videoView.start();
}
});
MediaController Class
public class ConstantAnchorMediaController extends MediaController {
public ConstantAnchorMediaController(Context context, View anchor) {
super(context);
super.setAnchorView(anchor);
}
@Override
public void setAnchorView(View view) {
// Do nothing
}
}
study_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" />
</RelativeLayout>
希望对大家有所帮助。
我正在开发一个 android 应用程序,我试图在其中播放来自 URL 的视频。
下面提到xml中的视频视图,
<VideoView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
在 ViewPager 内的片段中,来自 xml 的 VideoView 引用被调用并附加到 MediaController
videoView = (VideoView) studyVideo.findViewById(R.id.VideoView);
mediaController = new MediaController(getActivity());
mediaController.setAnchorView(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(studyVideoUrl));
videoView.requestFocus();
videoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
isVideoBuffered=true;
videoView.start();
}
});
我可以显示如下视频,
在这个截图中,MediaController
显示在Activity
的底部。但是,我需要像在Youtube中那样显示在VideoView的底部。
请帮我找到解决办法。
更新:
我已经在 xml 中添加了 MediaController,如下所示,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_above="@+id/mediaController"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<MediaController
android:id="@+id/mediaController"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</MediaController>
</RelativeLayout>
在Java,我更新了两个地方,
已替换
mediaController = new MediaController(getActivity());
与(MediaController) studyVideo .findViewById(R.id.mediaController);
在
onPrepared(MediaPlayer mp)
方法中添加了mediaController.show();
但是,应用程序崩溃并出现以下 logcat 错误,
07-06 00:25:25.936: E/MediaPlayer(32614): setDataSource: IOException! uri=https://ia700401.us.archive.org/19/items/ksnn_compilation_master_the_internet/ksnn_compilation_master_the_internet_512kb.mp4
07-06 00:25:35.331: E/AndroidRuntime(32614): FATAL EXCEPTION: main
07-06 00:25:35.331: E/AndroidRuntime(32614): java.lang.NullPointerException
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.widget.MediaController.show(MediaController.java:435)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.widget.MediaController.show(MediaController.java:394)
07-06 00:25:35.331: E/AndroidRuntime(32614): at com.prime.soft.fragments.StudyVideoFragment.onPrepared(StudyVideoFragment.java:70)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.widget.VideoView.onPrepared(VideoView.java:399)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.media.MediaPlayer$EventHandler.handleMessage(MediaPlayer.java:2125)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.os.Handler.dispatchMessage(Handler.java:107)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.os.Looper.loop(Looper.java:194)
07-06 00:25:35.331: E/AndroidRuntime(32614): at android.app.ActivityThread.main(ActivityThread.java:5391)
07-06 00:25:35.331: E/AndroidRuntime(32614): at java.lang.reflect.Method.invokeNative(Native Method)
07-06 00:25:35.331: E/AndroidRuntime(32614): at java.lang.reflect.Method.invoke(Method.java:525)
07-06 00:25:35.331: E/AndroidRuntime(32614): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
07-06 00:25:35.331: E/AndroidRuntime(32614): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
07-06 00:25:35.331: E/AndroidRuntime(32614): at dalvik.system.NativeStart.main(Native Method)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<MediaController
android:id="@+id/mediaController"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
</MediaController>
<VideoView
android:id="@+id/VideoView"
android:layout_above="@+id/mediaController"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
而且应该是这个
mediaController = (MediaController)
.findViewById(R.id.mediaController);
而不是这个,
(MediaController) studyVideo
.findViewById(R.id.mediaController);
或者您是否碰巧更改了 mediaPlayer member 变量的名称?
最后我通过扩展 MediaController
更新代码:
videoView = (VideoView) studyVideo.findViewById(R.id.VideoView);
mediaController = new ConstantAnchorMediaController(getActivity(),
videoView);
try {
mediaController.setAnchorView(videoView);
mediaController.setMediaPlayer(videoView);
videoView.setMediaController(mediaController);
videoView.setVideoURI(Uri.parse(studyVideoUrl));
} catch (Exception e) {
AlertMessage.showAlertMessage(getActivity(), e.toString());
}
videoView.setOnPreparedListener(new OnPreparedListener() {
// Close the progress bar and play the video
public void onPrepared(MediaPlayer mp) {
progressDialog.dismiss();
isVideoBuffered = true;
videoView.requestFocus();
videoView.start();
}
});
MediaController Class
public class ConstantAnchorMediaController extends MediaController {
public ConstantAnchorMediaController(Context context, View anchor) {
super(context);
super.setAnchorView(anchor);
}
@Override
public void setAnchorView(View view) {
// Do nothing
}
}
study_video.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/parent" >
<VideoView
android:id="@+id/VideoView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_marginTop="15dp" />
</RelativeLayout>
希望对大家有所帮助。