This is my code:
package com.testappmobile;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class testappmobileActivity extends Activity
{
    final Activity activity = this;
    private WebView webview;
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        // Check if the key event was the BACK key and if there's history
        if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
            webview.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 void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
        setContentView(R.layout.main);
        WebView webView = (WebView) findViewById(R.id.webview);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);
                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                view.loadUrl(url);
                return true;
            }
        });
        webView.loadUrl("http://developer.android.com/index.html");
    }
}
Now im having trouble getting the hardware back button to work. The app loads fine as does page and everything else but as soon as i hit the phones back button it crashes then forces close. I've searched google for the last 3 hours and have only found vague answers with very little info or broken links. Google's instructions sucked also as i am a beginner and they presume you know a certain amount.
Where should I place the code if it is in the wrong place?
Are there errors?
Cheers!
You have created a webView reference at class level but never initialized it thats why NullPointerException. I've made a small change in the onCreate method in your code, analyze it and make the necessary change(s);
@Override
public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
                setContentView(R.layout.main);
                // Don't create another webview reference here,
                // just use the one you declared at class level.
                webView = (WebView) findViewById(R.id.webView);
                webView.getSettings().setJavaScriptEnabled(true);
                webView.setWebChromeClient(new WebChromeClient() {
                    public void onProgressChanged(WebView view, int progress)
                    {
                        activity.setTitle("Loading...");
                        activity.setProgress(progress * 100);
                        if(progress == 100)
                            activity.setTitle(R.string.app_name);
                    }
                });
          // rest of code same
          // ...
          // ...
          // ...
}
Hope you got it. :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With