0

我的应用程序使用 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他的代码片段中的变量是什么,我我仍然卡住了。
我的问题是,有没有办法解决这个错误。是,如何?
提前致谢。 这是这里的堆栈跟踪

4

2 回答 2

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;
        }
    }
}
于 2012-09-27T14:49:31.180 回答
1

谢谢楼上的代码

楼主的问题我遇到了。</p>

在Activity中我是这样解决的。</p>

@Override
public void onBackPressed() {
    Log.e(TAG, "onBackPressed");

    if(mCustomView!=null){
        mFull.onHideCustomView();
    }else{
        super.onBackPressed();
    }
}
于 2013-10-22T13:04:02.517 回答