Android webview上一页的后退按钮无法正常工作

时间:2013-11-25 09:50:16

标签: android webview back-button

我正在尝试覆盖后退按钮,以便它返回上一页,但它只是退出应用程序。这里;代码..请告诉我哪里出错了。

package com.example.thebase;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.KeyEvent;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class MainAccelerometer extends Activity implements AccelerometerListener{

WebView myWebView;
//Back Button Code
     @Override
        public boolean onKeyDown(int keyCode, KeyEvent event) {
            if(event.getAction() == KeyEvent.ACTION_DOWN){
                switch(keyCode)
                {
                case KeyEvent.KEYCODE_BACK:
                    if(myWebView.canGoBack() == true){
                        myWebView.goBack();
                    }else{
                        finish();
                    }
                    return true;
                }

            }
            return super.onKeyDown(keyCode, event);
        }


//--------------------  


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.accelerometer_example_main);
    //ENABLING JAVASCRIPT CODE
            WebView myWebView = (WebView) findViewById(R.id.webview);
            WebSettings webSettings = myWebView.getSettings();
            webSettings.setJavaScriptEnabled(true);


            //WEBVIEW CODE
              myWebView = (WebView) findViewById(R.id.webview);
            myWebView.loadUrl("http://www.pingcampus.com/greedy/get_category.php");


            //WebView client
              myWebView.setWebViewClient(new HelloWebViewClient());

              //Back Button

              //------


        };
//rest of the code

2 个答案:

答案 0 :(得分:21)

您要覆盖onBackPressed()方法,而不是onKeyDown()。将onKeyDown()替换为onBackPressed(),如下所示:

@Override
public void onBackPressed() {
    if (myWebView.canGoBack()) 
        myWebView.goBack();
     else 
        super.onBackPressed();

}

修改

同时检查onCreate()

中的第3行
WebView myWebView = (WebView) findViewById(R.id.webview);

您再次将myWebView声明为局部变量,并且未实例化全局变量,这会抛出NullPointerException并导致应用崩溃。

所以改变你的第3行代码如下:

myWebView = (WebView) findViewById(R.id.webview);

答案 1 :(得分:2)

无需检查ACTION_DOWN。只是:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    // Check if the key event was the Back button and if there's history
    if( mWebView != null ) {
        if( (keyCode == KeyEvent.KEYCODE_BACK) && (mWebView.canGoBack()) ) {
            mWebView.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);
}

或者替代onKeyDown(),您可以覆盖onBackPressed()

@Override
public void onBackPressed() {
    if( mWebView != null ) {
        if( mWebView.canGoBack() ) {
            mWebView.goBack();
            return;
        }
    }

    return super.onBackPressed();
}