【问题标题】:Using the Back Button on WebView使用 WebView 上的后退按钮
【发布时间】:2013-12-15 00:58:57
【问题描述】:

我无法弄清楚为什么当我按下手机上的返回按钮后应用程序不断崩溃。我试图在 WebView 上使用后退按钮返回页面

这是我的代码:

package com.***.****;

import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;


public class MainActivity extends Activity {

WebView myWebView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
    setContentView(R.layout.activity_main);

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    myWebView.loadUrl("*****");
}

 @Override
 public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    // If it wasn't the Back key or there's no web page history, bubble up to the default
    // system behavior (probably exit the activity)
    return super.onKeyDown(keyCode, event);
}
}

当我点击后退按钮时会出现这种情况

11-28 14:13:17.768: E/AndroidRuntime(3636): FATAL EXCEPTION: main
11-28 14:13:17.768: E/AndroidRuntime(3636): java.lang.NullPointerException
11-28 14:13:17.768: E/AndroidRuntime(3636):     at com.triplec.googledeveloper.MainActivity.onKeyDown(MainActivity.java:35)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.view.KeyEvent.dispatch(KeyEvent.java:2756)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.app.Activity.dispatchKeyEvent(Activity.java:2428)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:2076)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:4192)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:4121)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3169)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.os.Looper.loop(Looper.java:137)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at android.app.ActivityThread.main(ActivityThread.java:5328)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at java.lang.reflect.Method.invokeNative(Native Method)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at java.lang.reflect.Method.invoke(Method.java:511)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
11-28 14:13:17.768: E/AndroidRuntime(3636):     at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 您可以放置​​导致应用程序崩溃的异常的堆栈跟踪吗?
  • 我刚做了,谢谢
  • 看起来它在 if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) 上崩溃了。您需要调试并找出 myWebView 变为 null 的原因。

标签: android eclipse webview back-button-control


【解决方案1】:

这是因为 myWebView 为空。一个快速的解决方法是:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if(myWebView != null){
        // Check if the key event was the Back button and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        // If it wasn't the Back key or there's no web page history, bubble up to the default
        // system behavior (probably exit the activity)
    }
    else{
        Log.e(MainActivity.class.getSimpleName(), "myWebView is null : won't do anything");
    }
    return super.onKeyDown(keyCode, event);
}

【讨论】:

  • 你需要在if语句之外调用return super.onKeyDown。
  • 完成。感谢您的评论。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-02
  • 1970-01-01
  • 1970-01-01
  • 2018-03-29
  • 1970-01-01
相关资源
最近更新 更多