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 :(