我的应用程序使用WebView显示Flash视频内容。
一切似乎都很顺利
直到您尝试在Android ICS设备上播放全屏。
在较低版本的设备上工作正常
在ICS设备上,它会抛出NullPointerException。
这是我的代码:
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
webView.setVerticalScrollbarOverlay(true);
webView.setWebViewClient(new WebViewClient() {
private ProgressDialog pd;
@Override
public void onPageStarted(WebView view, String url,
Bitmap favicon) {
pd = new ProgressDialog(TrainingDetailActivity.this);
pd.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pd.setMessage("Loading");
pd.show();
super.onPageStarted(view, url, favicon);
}
@Override
public void onPageFinished(WebView view, String url) {
this.dismissDialog();
super.onPageFinished(view, url);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view,
String url) {
return false;
}
private void dismissDialog() {
if (pd != null) {
pd.dismiss();
pd = null;
}
}
});
webView.loadDataWithBaseURL(baseUrl, htmlstr, "text/html", "utf-8", null);
经过一番挖掘,我发现在ICS中,android.webkit.PluginFullScreenHolder show()方法被调用并抛出 一个NullExceptionPointer。问题出在那里,而不是我的代码。 我尝试了一些工作,但没有一个工作 - 解决方法:我添加了这一行:
webView.setWebChromeClient(new WebChromeClient() );
使用NullPointerException进行此工作不会发生,但视频播放没有声音,并且不会切换到
全屏模式。
我查看了stackoverflow的解决方案,最接近我似乎解决了我的问题是这个答案:
https://stackoverflow.com/a/9921073/1503155
但不幸的是,回答者没有解释他的代码片段中的变量base
是什么,而我仍然被卡住了。
我的问题是,有没有办法解决这个bug。是的,如何?
提前致谢。
答案 0 :(得分:4)
我的老板允许我与你分享。
在将这些问题拼凑在一起之前,我有相同的问题。
将其创建为一个类,并设置WebView的chrome客户端:
WebView.setWebChromeClient(...);
对此:
public class FullscreenableChromeClient extends WebChromeClient {
protected Activity mActivity = null;
private View mCustomView;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
private int mOriginalOrientation;
private FrameLayout mContentView;
private FrameLayout mFullscreenContainer;
private static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
public FullscreenableChromeClient(Activity activity) {
this.mActivity = activity;
}
@Override
public void onShowCustomView(View view, int requestedOrientation, WebChromeClient.CustomViewCallback callback) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
if (mCustomView != null) {
callback.onCustomViewHidden();
return;
}
mOriginalOrientation = mActivity.getRequestedOrientation();
FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
mFullscreenContainer = new FullscreenHolder(mActivity);
mFullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
decor.addView(mFullscreenContainer, COVER_SCREEN_PARAMS);
mCustomView = view;
setFullscreen(true);
mCustomViewCallback = callback;
mActivity.setRequestedOrientation(requestedOrientation);
}
super.onShowCustomView(view, requestedOrientation, callback);
}
@Override
public void onHideCustomView() {
if (mCustomView == null) {
return;
}
setFullscreen(false);
FrameLayout decor = (FrameLayout) mActivity.getWindow().getDecorView();
decor.removeView(mFullscreenContainer);
mFullscreenContainer = null;
mCustomView = null;
mCustomViewCallback.onCustomViewHidden();
mActivity.setRequestedOrientation(mOriginalOrientation);
}
private void setFullscreen(boolean enabled) {
Window win = mActivity.getWindow();
WindowManager.LayoutParams winParams = win.getAttributes();
final int bits = WindowManager.LayoutParams.FLAG_FULLSCREEN;
if (enabled) {
winParams.flags |= bits;
} else {
winParams.flags &= ~bits;
if (mCustomView != null) {
mCustomView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
} else {
mContentView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);
}
}
win.setAttributes(winParams);
}
private static class FullscreenHolder extends FrameLayout {
public FullscreenHolder(Context ctx) {
super(ctx);
setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
}
@Override
public boolean onTouchEvent(MotionEvent evt) {
return true;
}
}
}
答案 1 :(得分:1)
感谢楼上的代码
我遇到了主持人的问题。
在活动中我像这样重新开始。
@Override
public void onBackPressed() {
Log.e(TAG, "onBackPressed");
if(mCustomView!=null){
mFull.onHideCustomView();
}else{
super.onBackPressed();
}
}