onKeyDown 方法导致应用程序崩溃

发布于 2024-12-11 20:45:42 字数 2276 浏览 0 评论 0原文

我有以下正在运行的代码,但是当我点击硬件后退按钮时,应用程序崩溃了,

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

我发现我需要

WebView webView;

在 onCeate 方法上方添加某个地方,该方法允许 webView 解析。所以现在我没有收到任何错误,但它崩溃了,应用程序在我的手机上运行,​​但当我按下硬件后退按钮时崩溃了。

有谁知道为什么会发生这种情况?

干杯,

迈克。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南冥有猫 2024-12-18 20:45:42

我会尝试对错误消息做我的魔术嘉宾。我认为,这个错误是由初始化 webView 变量的方式引起的。您会看到,在类级别上,您声明:

WebView webView;

然后,在 onCreate() 上初始化 webView 并再次重新声明它:

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

但是,此重新声明步骤导致了类级别webView 未初始化。

要修复它,请尝试将这一行更改为:

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

Into:

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

现在您应该已初始化类级别 webView 并准备好在您拥有的任何方法中使用。

以下是解释 Java 变量作用域的一些资源:

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:

蓝天白云 2024-12-18 20:45:42

您正在 onCreate() 方法中创建一个新的 Webview,而不是 Class 字段 Webview。变成

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

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

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文