在VideoView上无效

时间:2015-08-11 00:16:41

标签: java android nullpointerexception android-videoview

我正在制作一个视频播放器,用于从服务器流式传输视频。我已经检查过,因此视频网址是正确的并且有效!现在已经很晚了,所以我可能错过了一些东西,但为什么会这样呢

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)

2 个答案:

答案 0 :(得分:2)

videoView为空,因为您的父视图(view)没有与VideoView中的资源ID匹配的R.id.videoView,因此调用{{1}返回null。

我在您的异常日志中注意到您使用的是view.findViewById(R.id.videoView)。请记住,为了访问片段视图,您应该在findViewById之前对片段上的布局进行充气:

ViewPager

如果您确定@Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_recyclerview, container, false); } 是VideoView的正确标记,请找到view对象指向的布局并添加:

view

然后<VideoView android:id="@+id/videoView" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 现在应该正确返回声明的视图并为视频充气。

如果你的VideoView应该位于其他地方,那么你必须从它的正确父母那里充气视频:

VideoView videoView = (VideoView) view.findViewById(R.id.videoView)

希望这有帮助。

答案 1 :(得分:1)

我假设视图videoView存在于布局文件中,否则会引发编译错误。我在哪里可以问上面的代码?你可以发布其他背景吗?

知道在调用setContentView(R.layout.your_layout_id)之前需要调用findViewById()来设置布局,否则该调用将只返回null