android – 在4.0.4中删除包含videoView的片段时出错

我有一个包含VideoView和其他一些视图的片段.

public class PlayerPane extends Fragment { 
    ... // static variables
    private ImageView imageView;
    private ImageView gifView;
    private VideoView videoView;
    private WebView webView;
    private PDFView pdfView;
    private MyScrollTextView scrollTextView;
    private MediaPlayer audioPlayer;
    ...

    @Override
    public void onDestroyView() {
        if (videoView != null && videoView.isPlaying()) {
            LOGGER.info("Stopping videoView");
            videoView.stopPlayback();
            videoView.suspend();
            videoView = null;
        }
        super.onDestroyView();
    }

每当我在播放视频时删除此片段时,仅在Android 4.0.4中抛出IllegalStateException(4.0.4以上版本没有问题).

以下是例外:

java.lang.IllegalStateException
    at android.media.MediaPlayer._reset(Native Method)
    at android.media.MediaPlayer.reset(MediaPlayer.java:1236)
    at android.widget.VideoView.release(VideoView.java:549)
    at android.widget.VideoView.access$2300(VideoView.java:49)
    at android.widget.VideoView$6.surfaceDestroyed(VideoView.java:537)
    at android.view.SurfaceView.updateWindow(SurfaceView.java:581)
    at android.view.SurfaceView.onWindowVisibilityChanged(SurfaceView.java:290)
    at android.view.View.dispatchDetachedFromWindow(View.java:9823)
    at android.view.ViewGroup.dispatchDetachedFromWindow(ViewGroup.java:2266)
    at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3588)
    at android.view.ViewGroup.removeViewInternal(ViewGroup.java:3568)
    at android.view.ViewGroup.removeView(ViewGroup.java:3516)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:951)
    at android.app.FragmentManagerImpl.removeFragment(FragmentManager.java:1123)
    at android.app.BackStackRecord.run(BackStackRecord.java:592)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1382)
    at android.app.FragmentManagerImpl$1.run(FragmentManager.java:426)
    at android.os.Handler.handleCallback(Handler.java:605)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4424)
    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:784)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)

这是我删除片段的方法:

getFragmentManager.beginTransaction().remove(fragmentToBeRemoved).commit();

导致此IllegalStateException的原因是什么?如何解决此问题?
任何指向解决方案的东西都会有很大的帮助.

最佳答案 我建议你采取不同的方法.删除片段之前.在尝试删除片段之前调用此代码.

videoView.stopPlayback();
videoView.suspend();

从堆栈跟踪中可以看出,当您删除片段时,Android所做的就是删除VideoView(毕竟它是一个视图),这会导致Videoview尝试释放底层的Mediaplayer.但Mediaplayer本身可能不是一个导致这个问题的被释放的州.理想情况下,这个井可能已在VideocView中处理,但看起来没有

点赞