user1443402
user1443402

Reputation: 33

Android Webview - Back Button not working

I have a webview where the back button is not functioning and causing nullpointer exceptions. I am using a single activity and checking the intent extras for the url to load. It all functions normal for opening links, just the back button does not go back to previous page in webview instead it crashes the activity.

EDITTED: updated code block to below suggestion. Still gives null pointer exception

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView; 
import android.webkit.WebViewClient;

public class WebUrl extends Activity {

WebView myWebview; 

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle extras = getIntent().getExtras();

if(extras != null) {
    String link = extras.getString("url");
    WebView myWebview = (WebView) findViewById(R.id.webEngine);

    myWebview.getSettings().setJavaScriptEnabled(true);
    myWebview.getSettings().setSupportZoom(true) ; 
    myWebview.getSettings().setUseWideViewPort(true) ; 
    myWebview.getSettings().setLoadWithOverviewMode(true);
    myWebview.getSettings().setBuiltInZoomControls(true);
    myWebview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
    myWebview.setScrollbarFadingEnabled(false);

    // Load the URLs inside the WebView, not in the external web browser
    myWebview.setWebViewClient(new WebViewClient());

               if (savedInstanceState == null) {
            // Load a page
            myWebview.loadUrl(link);
        }
        }
    }

 @Override
 public void onBackPressed() {
    if(myWebview.canGoBack() == true) {
        myWebview.goBack();
    } else {
        WebUrl.super.onBackPressed(); //Replace MyActivity With the name of your activity.
    }
 }
 }

Logcat Errors:

07-01 11:54:19.817: E/AndroidRuntime(10079): FATAL EXCEPTION: main
07-01 11:54:19.817: E/AndroidRuntime(10079): java.lang.NullPointerException
07-01 11:54:19.817: E/AndroidRuntime(10079):    at com.app.browser.WebUrl.onBackPressed(WebUrl.java:70)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.app.Activity.onKeyUp(Activity.java:2163)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.view.KeyEvent.dispatch(KeyEvent.java:2651)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.app.Activity.dispatchKeyEvent(Activity.java:2393)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1853)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3683)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3653)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2893)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.os.Looper.loop(Looper.java:137)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at android.app.ActivityThread.main(ActivityThread.java:5059)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at java.lang.reflect.Method.invoke(Method.java:511)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-01 11:54:19.817: E/AndroidRuntime(10079):    at dalvik.system.NativeStart.main(Native Method)

Any help would be appreciated.

Thanks,

Upvotes: 1

Views: 3295

Answers (3)

invertigo
invertigo

Reputation: 6438

WebView myWebview = (WebView) findViewById(R.id.webEngine);

should be

myWebview = (WebView) findViewById(R.id.webEngine);

You are declaring a new WebView in your if block, then trying to reference the class variable myWebView (which was never instantiated) in the onBackPressed(). You can also use this.myWebView in all cases if you want to be extra sure that you are always using the class variable.

Upvotes: 3

David Freitag
David Freitag

Reputation: 2272

For starters your onBackPressed method is outside your class. That's why it's throwing a null pointer. I'm actually really suprised this even compiled and ran.

But you are way over complicating this, you do not need to replicate all of that code for two separate web pages. It looks like the settings you have for both Google and Mozilla are the same, in this case i would use this code:

import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class WebUrl extends Activity {

    WebView myWebview;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);
        Bundle extras = getIntent().getExtras();

        if(extras != null) {
            String link = extras.getString("url");
            WebView myWebview = (WebView) findViewById(R.id.webEngine);

            myWebview.getSettings().setJavaScriptEnabled(true);
            myWebview.getSettings().setSupportZoom(true) ; 
            myWebview.getSettings().setUseWideViewPort(true) ; 
            myWebview.getSettings().setLoadWithOverviewMode(true);
            myWebview.getSettings().setBuiltInZoomControls(true);
            myWebview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
            myWebview.setScrollbarFadingEnabled(false);

            // Load the URLs inside the WebView, not in the external web browser
            myWebview.setWebViewClient(new WebViewClient());

            if (savedInstanceState == null) {
                // Load a page
                myWebview.loadUrl(link);
            }
        }

        @Override
        public void onBackPressed() {
            if(myWebview.canGoBack() == true) {
                myWebview.goBack();
            } else {
                MyActivity.super.onBackPressed(); //Replace MyActivity With the name of your activity.
            }
        }
    }
}

From there, you can change specific settings based on what webpage users go to.

Upvotes: 1

stinepike
stinepike

Reputation: 54672

myWebview is not initialized properly.

myWebView is initializing when the following two conditions are true

 if(extras !=null)

and

 if(link.equals("http://www.google.com"))

So somehow any of these two condition is false. So the variable is not intialized. As a result it is null and throwing the exception

Upvotes: 0

Related Questions