单击VideoView时出现空指针异常

时间:2015-01-15 18:40:24

标签: java android android-videoview

我正在使用杰出的库,我在其中添加了一个videoView,一切正常运行,除非我点击视频然后mediaController不显示,我得到空指针异常。 这是我的代码: -

final LayoutInflater inflater = (LayoutInflater)    
 getSystemService(LAYOUT_INFLATER_SERVICE);
    final View view = inflater.inflate(R.layout.simple, frame, true);
    localVideoView = (VideoView) view.findViewById(R.id.videoView1);
    MediaController mediaController = new MediaController(StandOutExampleActivity.getActivity());
    mediaController.setAnchorView(localVideoView);
    localVideoView.setMediaController(mediaController);
    localVideoView.setVideoPath("android.resource://" + getPackageName() + "/" 
            + R.raw.ba);
    localVideoView.start();

日志文件: -

    67: E/AndroidRuntime(1997): FATAL EXCEPTION: main
    01-14 19:48:56.767: E/AndroidRuntime(1997): java.lang.NullPointerException
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.View.onAttachedToWindow(View.java:11709)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.View.dispatchAttachedToWindow(View.java:12125)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.ViewGroup.dispatchAttachedToWindow(ViewGroup.java:2450)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1207)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.Choreographer.doFrame(Choreographer.java:532)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.os.Handler.handleCallback(Handler.java:730)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.os.Handler.dispatchMessage(Handler.java:92)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.os.Looper.loop(Looper.java:137)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at android.app.ActivityThread.main(ActivityThread.java:5103)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at java.lang.reflect.Method.invokeNative(Native Method)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at java.lang.reflect.Method.invoke(Method.java:525)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    01-14 19:48:56.767: E/AndroidRuntime(1997):     at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

如果您的应用加载并播放视频正常但在您点击屏幕时崩溃,则最可能的原因是您的VideoView不知道如何处理触摸事件。尝试在代码中添加以下内容:

localVideoView.setOnTouchListener(new View.OnTouchListener()
{
    @Override
    public boolean onTouch(View v, MotionEvent event)
    {
        // Execute action on touch event
        return true;
    }
});

上面的代码片段为触摸事件设置了一个侦听器,以便它们不会被处理。