Android 在方向更改期间,片段中的VideoView为空
我的片段中有一个视频视图。当播放视频并且我更改方向时,它会在frgament onPause()中引发空指针异常,我正在暂停videview。此问题以前在将视频视图附加到活动时没有出现。最近,我不得不将活动更改为片段,并从中获取异常 video.xmlAndroid 在方向更改期间,片段中的VideoView为空,android,android-fragments,android-videoview,onpause,Android,Android Fragments,Android Videoview,Onpause,我的片段中有一个视频视图。当播放视频并且我更改方向时,它会在frgament onPause()中引发空指针异常,我正在暂停videview。此问题以前在将视频视图附加到活动时没有出现。最近,我不得不将活动更改为片段,并从中获取异常 video.xml <FrameLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/fullScr
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/fullScreenContainer">
<SurfaceView
android:layout_width="0px"
android:layout_height="0px"
android:id="@+id/dummySurfaceView"/>
</FrameLayout>
videoView是类Video的对象,它扩展了RelativeLayout,其中包含videoView vidPlayer
Video.java
public void pausePlayer() {
isPaused = true;
vidPlayer.pause();
btnPlay.setImageResource(R.drawable.video_play);
controlsManager.cancelHideTimer();
}
这就是结构->
___________________________________
| ** MyFragment.java ** |
| _______________________________ |
| | ** Video.java ** | |
| | ___________________________ | |
| | | | | |
| | | **VideoView vidPlayer** | | |
| | |___________________________| | |
| | | |
| |_______________________________| |
| |
|___________________________________|
在活动的清单上尝试此加载项:
android:configChanges="keyboardHidden|orientation"
在您的活动中,您必须添加以下代码:
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
}
我建议你在视频中只使用横向定位,这样可以解决你的问题。在视频播放器活动的清单中使用android:Orientation=“横向”。否则,您需要在配置中添加一些代码,而不仅仅是我发布的代码。在本例中,我使用mDrawerToggle.onConfigurationChanged(newConfig)保存抽屉的配置;尝试vidPlayer.onConfigurationChanged(newConfig);不确定它是否有效(希望这有帮助),如果你最终使用它,不要忘记投票并选择我的答案。
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggls
}