canGoBack()在我的第二个活动

时间:2016-08-25 11:38:17

标签: android webview

我创建了第二个活动,我有一个网页视图,所以我使用这个代码从网站内容返回到主页,但如果我按后退按钮应用程序停止工作,任何人都可以帮助我 ?

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (mWebView.canGoBack()) { 
                    mWebView.goBack();
                } else {
                    finish();
                }
                return true;
            }
        }
    return super.onKeyDown(keyCode, event);
}

这是日志

  

08-25 18:00:48.805 24025-24025 / com.example.jaisonjoseph.newsclient   E / InputEventSender:异常调度完成信号。 08-25   18:00:48.805 24025-24025 / com.example.jaisonjoseph.newsclient   E / MessageQueue-JNI:MessageQueue回调中的异常:   handleReceiveCallback 08-25 18:00:48.806   24025-24025 / com.example.jaisonjoseph.newsclient E / MessageQueue-JNI:   java.lang.NullPointerException:尝试调用虚方法   ' boolean android.webkit.WebView.canGoBack()'在null对象上   参考                                                                                              在   com.example.jaisonjoseph.newsclient.Manorama.onKeyDown(Manorama.java:38)                                                                                              在android.view.KeyEvent.dispatch(KeyEvent.java:2655)                                                                                              在android.app.Activity.dispatchKeyEvent(Activity.java:2755)                                                                                              在   com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:2380)                                                                                              在   android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4558)                                                                                              在   android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4514)                                                                                              在   android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4036)                                                                                              在   android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4089)                                                                                              在   android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4055)                                                                                              在   android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4192)                                                                                              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4063)                                                                                              在   android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4249)                                                                                              在   android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4036)                                                                                              在   android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4089)                                                                                              在   android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4055)                                                                                              在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4063)                                                                                              在   android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4036)                                                                                              在   android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4089)                                                                                              在   android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4055)                                                                                              在   android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4225)                                                                                              在   android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4395)                                                                                              在   android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2299)                                                                                              在   android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1923)                                                                                              在   android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1914)                                                                                              在   android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2276)                                                                                              在   android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)                                                                                              在android.os.MessageQueue.nativePollOnce(本机方法)                                                                                              在android.os.MessageQueue.next(MessageQueue.java:148)                                                                                              在android.os.Looper.loop(Looper.java:151)                                                                                              在android.app.ActivityThread.main(ActivityThread.java:5706)                                                                                              at java.lang.reflect.Method.invoke(Native Method)                                                                                              在java.lang.reflect.Method.invoke(Method.java:372)                                                                                              在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1033)                                                                                              在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)08-25   18:00:48.808 24025-24025 / com.example.jaisonjoseph.newsclient   E / AndroidRuntime:致命异常:主要                                                                                        处理:com.example.jaisonjoseph.newsclient,PID:24025                                                                                        java.lang.NullPointerException:尝试调用虚方法   ' boolean android.webkit.WebView.canGoBack()'在null对象上   参考                                                                                            在   com.example.jaisonjoseph.newsclient.Manorama.onKeyDown(Manorama.java:38)                                                                                            在android.view.KeyEvent.dispatch(KeyEvent.java:2655)                                                                                            在android.app.Activity.dispatchKeyEvent(Activity.java:2755)                                                                                            在   com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:2380)                                                                                            在   android.view.ViewRootImpl $ ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:4558)                                                                                            在   android.view.ViewRootImpl $ ViewPostImeInputStage.onProcess(ViewRootImpl.java:4514)                                                                                            在   android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4036)                                                                                            在   android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4089)                                                                                            在   android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4055)                                                                                            在   android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4192)                                                                                            在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4063)                                                                                            在   android.view.ViewRootImpl $ AsyncInputStage.apply(ViewRootImpl.java:4249)                                                                                            在   android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4036)                                                                                            在   android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4089)                                                                                            在   android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4055)                                                                                            在android.view.ViewRootImpl $ InputStage.apply(ViewRootImpl.java:4063)                                                                                            在   android.view.ViewRootImpl $ InputStage.deliver(ViewRootImpl.java:4036)                                                                                            在   android.view.ViewRootImpl $ InputStage.onDeliverToNext(ViewRootImpl.java:4089)                                                                                            在   android.view.ViewRootImpl $ InputStage.forward(ViewRootImpl.java:4055)                                                                                            在   android.view.ViewRootImpl $ AsyncInputStage.forward(ViewRootImpl.java:4225)                                                                                            在   android.view.ViewRootImpl $ ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:4395)                                                                                            在   android.view.inputmethod.InputMethodManager $ PendingEvent.run(InputMethodManager.java:2299)                                                                                            在   android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1923)                                                                                            在   android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1914)                                                                                            在   android.view.inputmethod.InputMethodManager $ ImeInputEventSender.onInputEventFinished(InputMethodManager.java:2276)                                                                                            在   android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)                                                                                            在android.os.MessageQueue.nativePollOnce(本机方法)                                                                                            在android.os.MessageQueue.next(MessageQueue.java:148)                                                                                            在android.os.Looper.loop(Looper.java:151)                                                                                            在android.app.ActivityThread.main(ActivityThread.java:5706)                                                                                            at java.lang.reflect.Method.invoke(Native Method)                                                                                            在java.lang.reflect.Method.invoke(Method.java:372)                                                                                            在   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1033)                                                                                            在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:828)

1 个答案:

答案 0 :(得分:0)

您的webview对象未初始化。获取webview Id,如果是,则为null,然后按下getback键

@Override public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getAction() == KeyEvent.ACTION_DOWN) {
            switch (keyCode) {
                case KeyEvent.KEYCODE_BACK:
             WebView webView = (WebView) findViewById(R.id.myWebView);
            if(webView!=null){
                    if (mWebView.canGoBack()) { 
                        mWebView.goBack();
                    }
    } else {
                        finish();
                    }
                    return true;
                }
            }
        return super.onKeyDown(keyCode, event);
    }