简体   繁体   中英

onKeyDown method crashing app

I have the following code that is working but when I hit the hardware back button the app crashes,

package com.fnesse.tmpmobile;

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

@SuppressWarnings("unused")
public class TmpmobileActivity extends Activity 
{
WebView webView;
final Activity activity = this;    

    @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.setBackgroundColor(0x00000000);
        webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);

        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)
        {
            //Error Code Here
        }

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            view.loadUrl(url);
            return true;
        }

    });

    webView.loadUrl("http://www.themorningtonpeninsula.com");
    }

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

I found somewhere that I needed to add,

WebView webView;

Above the onCeate method, which allowed webView to resolve. So now I get no errors but it crashes and the app runs on my phone but crashes when I hit the hardware back button.

Does anyone know why that would be happening?

Cheers,

Mike.

I'll try to do my magic guest on the error message. I think, this error is caused by the way to initialize webView variable. You see, on the class level, you declare:

WebView webView;

And then, on onCreate() you initialize webView and also redeclare it again:

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

But, this redeclare step resulted in class level webView to be uninitialized.

To fix it, try to change this line:

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

Into:

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

And you should now have class level webView initialized and ready for use in whatever method you have.

Here's few resources explaining Java variable scope:

You are creating a new Webview in your onCreate() method instead of the Class field Webview. Change

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

into:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM