android webview CustomView onHideCustomView nullpointerexception

时间:2014-11-12 11:06:48

标签: android webview

您好我正在制作一个Android Web View应用,但我有错误。

我该如何修复此错误?

请帮帮我。

WebView myWebView;
private MyWebChromeClient mWebChromeClient = null;
private View mCustomView;
private RelativeLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams LayoutParameters = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
        FrameLayout.LayoutParams.MATCH_PARENT);

...

private class MyWebChromeClient extends WebChromeClient {

public void onHideCustomView() {
        if (mCustomView == null) {
            return;
        } else {
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN,WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
            // Hide the custom view.
            mCustomView.setVisibility(View.GONE);
            // Remove the custom view from its container.
            mCustomViewContainer.removeView(mCustomView);
            mCustomView = null;
            mCustomViewContainer.setVisibility(View.GONE);
            mCustomViewCallback.onCustomViewHidden();
            // Show the content view.
            mContentView.setVisibility(View.VISIBLE);
            super.onHideCustomView();
        }
    }
}

public boolean onKeyDown(int key, KeyEvent event) {
    if ((mCustomViewContainer != null)&&(key == KeyEvent.KEYCODE_BACK)) {
        mWebChromeClient.onHideCustomView();
        return true;
    }
    else if((key == KeyEvent.KEYCODE_BACK)&&myWebView.canGoBack()){
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(key, event);
}

Log Cat

11-12 19:30:56.175  20071-20085/com.bayabro.bayabro V/MediaPlayer﹕ callback application
11-12 19:30:56.175  20071-20085/com.bayabro.bayabro V/MediaPlayer﹕ back from callback
11-12 19:30:56.670  20071-20071/com.bayabro.bayabro D/AndroidRuntime﹕ Shutting down VM
11-12 19:30:56.670  20071-20071/com.bayabro.bayabro W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x416992a0)
11-12 19:30:56.670  20071-20071/com.bayabro.bayabro E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.NullPointerException
            at com.bayabro.bayabro.MyActivity.onKeyDown(MyActivity.java:169)
            at android.view.KeyEvent.dispatch(KeyEvent.java:2705)
            at android.app.Activity.dispatchKeyEvent(Activity.java:2431)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2028)
            at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3852)
            at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3800)
            at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2935)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            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:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
11-12 19:30:57.175  20071-20086/com.bayabro.bayabro V/MediaPlayer﹕ message received msg=3, ext1=63, ext2=0

1 个答案:

答案 0 :(得分:0)

永远不会初始化

myWebView,因此&&myWebView.canGoBack()为空。

请务必致电myWebView

初始化myWebView = (WebView) findViewById(R.id.webview)

避免NPE:

else if(myWebView != null && key == KeyEvent.KEYCODE_BACK && myWebView.canGoBack()){
    myWebView.goBack();
    return true;
}