VideoView 为空
Null on VideoView
我正在制作一个视频播放器,可以从服务器流式传输视频。我已经检查过视频 url 是正确的并且有效!真的很晚了,所以我可能错过了一些东西,但为什么会这样
VideoView videoView = (VideoView) view.findViewById(R.id.videoView);
Uri video = Uri.parse("www.xxx.xxx/video.mp4");
videoView.setVideoURI(video);
videoView.start();
给我这个错误
08-11 02:14:25.217 12390-12390/com.shredacademy.guitarmethodsintroductiontotheory E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.shredacademy.guitarmethodsintroductiontotheory, PID: 12390
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(android.net.Uri)' on a null object reference
at com.shredacademy.guitarmethodsintroductiontotheory.videoC.onViewCreated(videoC.java:56)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
at android.view.View.measure(View.java:17562)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2871)
at android.view.View.measure(View.java:17562)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
videoView
为空,因为您的父视图 (view
) 没有 VideoView
与 R.id.videoView
中的资源 ID 匹配,因此调用 view.findViewById(R.id.videoView)
returns null.
我在您的异常日志中注意到您正在使用 ViewPager
。请记住,为了访问片段视图,您应该在 findViewById 之前扩充片段上的布局:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
如果您确定 view
是 VideoView 的正确硬币容器,找到您的 view
对象指向的布局并添加:
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
然后 VideoView videoView = (VideoView) view.findViewById(R.id.videoView)
现在应该正确 return 声明的视图并膨胀你的 videoView。
如果你的 VideoView 应该位于其他地方,那么你必须从它的正确父级膨胀 videoView:
VideoView videoView = (VideoView) viewThatContainsVideoView.findViewById(R.id.videoView);
希望对您有所帮助。
我假设视图 videoView
存在于布局文件中,否则会引发编译错误。请问上面的代码在哪里?你能 post 额外的上下文吗?
知道在调用findViewById()
之前需要调用setContentView(R.layout.your_layout_id)
来设置布局,否则调用return只会null
.
我正在制作一个视频播放器,可以从服务器流式传输视频。我已经检查过视频 url 是正确的并且有效!真的很晚了,所以我可能错过了一些东西,但为什么会这样
VideoView videoView = (VideoView) view.findViewById(R.id.videoView);
Uri video = Uri.parse("www.xxx.xxx/video.mp4");
videoView.setVideoURI(video);
videoView.start();
给我这个错误
08-11 02:14:25.217 12390-12390/com.shredacademy.guitarmethodsintroductiontotheory E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.shredacademy.guitarmethodsintroductiontotheory, PID: 12390
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.VideoView.setVideoURI(android.net.Uri)' on a null object reference
at com.shredacademy.guitarmethodsintroductiontotheory.videoC.onViewCreated(videoC.java:56)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:973)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:490)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1105)
at android.support.v4.view.ViewPager.populate(ViewPager.java:951)
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1473)
at android.view.View.measure(View.java:17562)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.support.v7.internal.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:124)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:444)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1436)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:722)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:613)
at android.view.View.measure(View.java:17562)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5536)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2871)
at android.view.View.measure(View.java:17562)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2015)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1173)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1379)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5289)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)
videoView
为空,因为您的父视图 (view
) 没有 VideoView
与 R.id.videoView
中的资源 ID 匹配,因此调用 view.findViewById(R.id.videoView)
returns null.
我在您的异常日志中注意到您正在使用 ViewPager
。请记住,为了访问片段视图,您应该在 findViewById 之前扩充片段上的布局:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_recyclerview, container, false);
}
如果您确定 view
是 VideoView 的正确硬币容器,找到您的 view
对象指向的布局并添加:
<VideoView
android:id="@+id/videoView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
然后 VideoView videoView = (VideoView) view.findViewById(R.id.videoView)
现在应该正确 return 声明的视图并膨胀你的 videoView。
如果你的 VideoView 应该位于其他地方,那么你必须从它的正确父级膨胀 videoView:
VideoView videoView = (VideoView) viewThatContainsVideoView.findViewById(R.id.videoView);
希望对您有所帮助。
我假设视图 videoView
存在于布局文件中,否则会引发编译错误。请问上面的代码在哪里?你能 post 额外的上下文吗?
知道在调用findViewById()
之前需要调用setContentView(R.layout.your_layout_id)
来设置布局,否则调用return只会null
.