onKeyDown 方法导致应用程序崩溃
我有以下正在运行的代码,但是当我点击硬件后退按钮时,应用程序崩溃了,
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我会尝试对错误消息做我的魔术嘉宾。我认为,这个错误是由初始化
webView
变量的方式引起的。您会看到,在类级别上,您声明:然后,在
onCreate()
上初始化webView
并再次重新声明它:但是,此重新声明步骤导致了类级别
webView
未初始化。要修复它,请尝试将这一行更改为:
Into:
现在您应该已初始化类级别
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:And then, on
onCreate()
you initializewebView
and also redeclare it again:But, this redeclare step resulted in class level
webView
to be uninitialized.To fix it, try to change this line:
Into:
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:
您正在 onCreate() 方法中创建一个新的 Webview,而不是 Class 字段 Webview。变成
:
You are creating a new Webview in your onCreate() method instead of the Class field Webview. Change
into: