-1

Here is my program code. I am new to Android and Java so it may seem messy. When I press the back button after adding in this code i get a Force Close.

    package com.alexriggs.android.gansterstreetwars;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class GangsterStreetWarsActivity extends Activity {
    /** Called when the activity is first created. */
    final Activity activity = this;
    WebView myWebView;
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.getSettings().setJavaScriptEnabled(true);
        myWebView.setWebViewClient(new MyWebViewClient());
        myWebView.getSettings().setBuiltInZoomControls(true);
        myWebView.getSettings().setLoadWithOverviewMode(true);
        myWebView.getSettings().setUseWideViewPort(true);
        myWebView.loadUrl("http://mafia.itwontwork.net/?pk_campaign=Android&pk_kwd=MainApp");
    }
    private class MyWebViewClient extends WebViewClient {


        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals("mafia.itwontwork.net")) {
                // Designate Urls that you want to load in WebView still.
                return false;
            }           
            // Otherwise, give the default behavior (open in browser)
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }
        }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
    if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
        myWebView.goBack();
        return true;
    }
    return super.onKeyDown(keyCode, event);
    }
}

A logcat of the error is here: http://itwontwork.net/logcat/1.txt

I have narrowed it down to the line "myWebView.goBack();". It still does not work even when i take out all the Ifs and just leave that line.

I got the Back button code from Here and it worked for 18 people. But not me :(

Community
  • 1
  • 1
alexander7567
  • 665
  • 13
  • 35

2 Answers2

2

The problem is that you've not actually got an instance of the WebView in the myWebView member variable, only in the local variable in the onCreate method. (This is why you have a NullPointerException when the code runs - myWebView hasn't ever been initialized.)

You can fix it by changing the line

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

to

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

in onCreate. (I haven't checked for other errors, but this should fix your problem.)

not all wrong
  • 622
  • 7
  • 18
2

Write below line into your code

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

2)  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

instead of

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

2)  @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
            myWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128