Fernando
Fernando

Reputation: 460

Back button in Webview crashes app

So I'm trying to make an app that displays a webview. And that's working perfectly. But now I'm wanting to make it so when you click a link it remembers it and you can navigate back by clicking the back button. And I was following the Android Developers guide on it but my app crashes when I click the back button. Here's my code.

package com.example.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
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) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    WebView myWebView = (WebView) findViewById(R.id.webview);
    myWebView.setWebViewClient(new WebViewClient());
    myWebView.loadUrl("http://www.oxpheen.com");
    WebSettings webSettings = myWebView.getSettings();
    webSettings.setJavaScriptEnabled(true);

}

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

And my Activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:layout_width="fill_parent" 
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/webview"/> 
</RelativeLayout>

And here's the log in the logcat it produces

10-24 16:44:02.875: E/AndroidRuntime(25196): FATAL EXCEPTION: main
10-24 16:44:02.875: E/AndroidRuntime(25196): java.lang.NullPointerException
10-24 16:44:02.875: E/AndroidRuntime(25196):    at com.example.webview.MainActivity.onKeyDown(MainActivity.java:31)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1903)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.os.Looper.loop(Looper.java:137)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at android.app.ActivityThread.main(ActivityThread.java:5227)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-24 16:44:02.875: E/AndroidRuntime(25196):    at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 1393

Answers (1)

Sam Dozor
Sam Dozor

Reputation: 40734

Change this line:

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

to just:

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

The issue is that you're never setting the myWebView class member to the webview reference, instead you're setting a local variable within the scope of onCreate.

Upvotes: 3

Related Questions