Java 获取back键上的强制关闭错误
当我按下后退键离开这个网站的游戏时,我遇到了一个问题。我在back键上收到一条强制错误消息。我使用了我从第一个问题中学到的销毁代码。没有改变任何事情。有什么想法吗Java 获取back键上的强制关闭错误,java,android,Java,Android,当我按下后退键离开这个网站的游戏时,我遇到了一个问题。我在back键上收到一条强制错误消息。我使用了我从第一个问题中学到的销毁代码。没有改变任何事情。有什么想法吗 public class MainActivity extends Activity { WebView myWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);
public class MainActivity extends Activity {
WebView myWebView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView myWebView = (WebView) findViewById(R.id.webView1);
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadUrl("http://www.limejs.com/static/roundball/index.html");
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
finish();
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 onDestroy() {
super.onDestroy();
myWebView.destroy();
}
}
我会删除返回的true;就在完成之后() 如果您有一些adb日志来显示您得到的确切错误,这将有所帮助
您还可以检查myWebView是否为null。我想这也可能是空的 将代码更改为下面给出的代码。。基本上。。我将您对finish()的调用更新为MainActivity.this.finish(),其中MainActivity是您的活动!因此,您必须将MainActivity更改为您指定的任何名称。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
MainActivity.this.finish();
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);
}
你的日志上写着:
位于的java.lang.NullPointerException
com.example.roundball.MainActivity.onKeyDown(MainActivity.java:33)
这意味着在执行时onKeyDown方法内部的代码中有一个空引用。这里只使用了2个引用,但由于不太可能收到null KeyEvent(它来自Android运行时,文档隐式地说,调用onKeyDown时总是收到有效引用),因此它必须是对webview的引用。因此,请检查myWebView!=调用myWebView方法之前为null,如下所示
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if(myWebView != null) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
finish();
return true;
}
} else {
android.util.Log.w("MyActivity", "myWebView is null!!");
}
// 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);
}
从该代码中,我无法判断是什么原因导致您对webview的引用为空,但如果您希望webview引用不为空,它可能会告诉您下一步在哪里查找问题。显示日志,这就是我们可以看到问题的原因。我删除了返回true;同样的结果。我使用此代码访问网站,并将密钥返回,没有问题。这是一个游戏网站。正在做一些我不知道的奇怪的事情。我已进入航海日志。ThanksI使用相同的结果将您的行添加到MainActivity文件中。除了一个致命的异常错误,我不确定发生了什么。奇怪的是,main活动进入一个web站点并顺利退出。奇怪???我做的和你想要的很相似。。。顺便问一下,为什么需要myWebView.destroy();?您可能只需要super.ondestory();本身。。并且不删除myWebView.destroy()。。。
//replace for this
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
return true;
}
if (keyCode != KeyEvent.KEYCODE_BACK){
return MainActivity.onKeyDown(keyCode, event);
}else{
myWebView.destroy();
MainActivity.finish();
return false;
}
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// Check if the key event was the Back button and if there's history
if(myWebView != null) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && myWebView.canGoBack()) {
myWebView.goBack();
finish();
return true;
}
} else {
android.util.Log.w("MyActivity", "myWebView is null!!");
}
// 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);
}