单击 VideoView 时出现空指针异常

Null Pointer exception when I click on 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)

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

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

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