Android Webview onKeyDown强制关闭app

时间:2015-03-06 14:54:33

标签: android webview forceclose onkeydown

基本上我有一个简单的webview应用程序,我正在为大学项目做准备而且我对编码几乎一无所知。 webview按预期工作并加载仍在我的应用中的其他链接,但后退按钮不会导航到上一页,它只是强制关闭应用程序并弹出一条消息说“不幸的是,'布罗德本特工作室已经停止了#39;如果我只是删除后退按钮的代码,应用程序工作正常,但我不能像在Web浏览器中那样使用后退按钮,这会更好。

我尝试过不同的方法,但没有一种方法有效,这是最好的,因为eclipse没有错误,但显然有些错误。



package com.broadbentstudios;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;

public class BroadbentStudios extends Activity
{
    final Activity activity = this;


    WebView WebView;
    @SuppressLint("SetJavaScriptEnabled")
	public void onCreate(Bundle savedInstanceState)

    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.mainWebView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.loadUrl("http://www.bsstudios.portfoliobox.me");


        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });   
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && WebView.canGoBack()) {
            WebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}




Logcat条目



03-06 15:20:14.757: W/UnimplementedWebViewApi(32191): Unimplemented WebView method onKeyDown called from: android.webkit.WebView.onKeyDown(WebView.java:2178)
03-06 15:20:14.757: E/InputEventSender(32191): Exception dispatching finished signal.
03-06 15:20:14.757: E/MessageQueue-JNI(32191): Exception in MessageQueue callback: handleReceiveCallback
03-06 15:20:14.757: E/MessageQueue-JNI(32191): java.lang.NullPointerException
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at com.broadbentstudios.BroadbentStudios.onKeyDown(BroadbentStudios.java:42)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.app.Activity.dispatchKeyEvent(Activity.java:2423)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2017)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3848)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3822)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3521)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3578)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3554)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3714)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2021)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1715)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1706)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1998)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.os.MessageQueue.nativePollOnce(Native Method)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.os.MessageQueue.next(MessageQueue.java:138)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.os.Looper.loop(Looper.java:123)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at android.app.ActivityThread.main(ActivityThread.java:5146)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at java.lang.reflect.Method.invoke(Method.java:515)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
03-06 15:20:14.757: E/MessageQueue-JNI(32191): 	at dalvik.system.NativeStart.main(Native Method)
03-06 15:20:14.757: D/AndroidRuntime(32191): Shutting down VM
03-06 15:20:14.757: W/dalvikvm(32191): threadid=1: thread exiting with uncaught exception (group=0x415f9db8)
03-06 15:20:14.767: E/AndroidRuntime(32191): FATAL EXCEPTION: main
03-06 15:20:14.767: E/AndroidRuntime(32191): Process: com.broadbentstudios, PID: 32191
03-06 15:20:14.767: E/AndroidRuntime(32191): java.lang.NullPointerException
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at com.broadbentstudios.BroadbentStudios.onKeyDown(BroadbentStudios.java:42)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.KeyEvent.dispatch(KeyEvent.java:2640)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.app.Activity.dispatchKeyEvent(Activity.java:2423)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2017)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:3848)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3822)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3521)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3578)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3422)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3395)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3445)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3414)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3554)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.ViewRootImpl$ImeInputStage.onFinishedInputEvent(ViewRootImpl.java:3714)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.inputmethod.InputMethodManager$PendingEvent.run(InputMethodManager.java:2021)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.inputmethod.InputMethodManager.invokeFinishedInputEventCallback(InputMethodManager.java:1715)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.inputmethod.InputMethodManager.finishedInputEvent(InputMethodManager.java:1706)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.inputmethod.InputMethodManager$ImeInputEventSender.onInputEventFinished(InputMethodManager.java:1998)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.view.InputEventSender.dispatchInputEventFinished(InputEventSender.java:141)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.os.MessageQueue.nativePollOnce(Native Method)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.os.MessageQueue.next(MessageQueue.java:138)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.os.Looper.loop(Looper.java:123)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at android.app.ActivityThread.main(ActivityThread.java:5146)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at java.lang.reflect.Method.invokeNative(Native Method)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at java.lang.reflect.Method.invoke(Method.java:515)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
03-06 15:20:14.767: E/AndroidRuntime(32191): 	at dalvik.system.NativeStart.main(Native Method)




1 个答案:

答案 0 :(得分:2)

您希望使用全局变量(WebView WebView;)执行操作,但在WebView webView = (WebView) findViewById(R.id.mainWebView);中有另一个本地WebView变量(onCreate())。

所以修改你的代码:

public class BroadbentStudios extends Activity {

    WebView webView;
    @SuppressLint("SetJavaScriptEnabled")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);

        webView = (WebView) findViewById(R.id.mainWebView);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setUseWideViewPort(true);
        webView.loadUrl("http://www.bsstudios.portfoliobox.me");

        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webView!=null && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

我刚刚运行了上面的代码。我保证这段代码有效;)

祝你好运;)