我有一个基本上是“媒体”播放器的活动,它可以播放某些类型的内容(例如图像、视频、RSS 提要等),并且我使用不同的布局文件。我有一个播放列表,但硬件或仿真器编解码器不支持某些视频。因此,当我在尝试加载视频时遇到错误时,我只是移至下一个并尝试播放它。
问题是在我收到加载错误并开始播放下一个媒体之后。我的 videoPlayer 布局上的 VideoView 会引发 NPE 并使应用程序崩溃:S
我已经使用版本 4.1.2、4.2.2、4.3 和 2.3.3 在模拟器上进行了测试。所有 4.xx 版本都显示了问题...
这是尝试播放视频的片段。
...
videoView = (VideoView) getActivity().findViewById(R.id.videoview);
videoView.setVideoPath(currentMediaPath);
videoView.setOnErrorListener(new OnErrorListener()
{
@Override
public boolean onError(MediaPlayer player, int what, int extra)
{
String message = "error: [" + currentIndex + "] " + currentMediaPath + " what: " + what + " extra: "
+ extra;
L.error(getLogger(), "onError: " + player + " - " + message);
boolean delegated = false;
try
{
if (isPreparing() && delegateOnError)
{
delegatePlayback();
delegated = true;
}
}
catch (Exception e)
{
L.error(getLogger(), "onError: error delegating playback -> " + player, e);
}
if (!delegated)
{
notifyError(new Exception(message));
}
return true;
}
});
...
这是崩溃...
java.lang.NullPointerException
at android.view.SurfaceView.updateWindow(SurfaceView.java:574)
at android.view.SurfaceView.access$000(SurfaceView.java:81)
at android.view.SurfaceView$3.onPreDraw(SurfaceView.java:169)
at android.view.ViewTreeObserver.dispatchOnPreDraw(ViewTreeObserver.java:671)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1820)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)