我在此网站上按返回键离开游戏时遇到问题。我在返回键上收到强制错误消息。我使用了从第一个问题中学到的销毁代码。没有改变任何东西。有什么想法吗?
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();
}
Here is the Logcat:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.roundball.MainActivity.onKeyDown(MainActivity.java:33)
at android.view.KeyEvent.dispatch(KeyEvent.java:1256)
at android.app.Activity.dispatchKeyEvent(Activity.java:2078)
at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent
(PhoneWindow.java:1771)
at android.view.ViewRoot.deliverKeyEventToViewHierarchy(ViewRoot.java:2563)
at android.view.ViewRoot.handleFinishedEvent(ViewRoot.java:2538)
at android.view.ViewRoot.handleMessage(ViewRoot.java:1870)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3683)`enter code here`
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
at dalvik.system.NativeStart.main(Native Method)
I have used this code before to load web sites, it is just particular to this game?
最佳答案
我会删除 return true;就在 finish() 之后;
如果您有一些 adb 日志来显示您遇到的确切错误,将会有所帮助。
您还可以检查 myWebView 是否不为空。我认为这也可能为空。
关于java - 在后退键上出现强制关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21344558/