androideclipsewebviewback-button-control

Using the Back Button on WebView


I cant figure out why the application keeps crashing once i press the back button on my phone. Im trying to use the back button to go back a page while on the WebView

This is my code:

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);
}
}

This is what comes up when i click the back button

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)

Solution

  • It is because myWebView is null. A quick fix to it would be :

    @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);
    }