所以我正在尝试制作一个显示 webview 的应用程序。这是完美的工作。但现在我想做它,所以当你点击一个链接时它会记住它,你可以通过点击后退按钮导航回来。我一直在关注 Android 开发人员指南,但是当我单击后退按钮时,我的应用程序崩溃了。这是我的代码。
package com.example.webview;
import android.app.Activity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
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.webview);
myWebView.setWebViewClient(new WebViewClient());
myWebView.loadUrl("http://www.oxpheen.com");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
}
@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 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 boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
还有我的活动
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:id="@+id/webview"/>
</RelativeLayout>
这是它生成的 logcat 中的日志
10-24 16:44:02.875: E/AndroidRuntime(25196): FATAL EXCEPTION: main
10-24 16:44:02.875: E/AndroidRuntime(25196): java.lang.NullPointerException
10-24 16:44:02.875: E/AndroidRuntime(25196): at com.example.webview.MainActivity.onKeyDown(MainActivity.java:31)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.view.KeyEvent.dispatch(KeyEvent.java:2609)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.app.Activity.dispatchKeyEvent(Activity.java:2375)
10-24 16:44:02.875: E/AndroidRuntime(25196): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1903)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2818)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.os.Looper.loop(Looper.java:137)
10-24 16:44:02.875: E/AndroidRuntime(25196): at android.app.ActivityThread.main(ActivityThread.java:5227)
10-24 16:44:02.875: E/AndroidRuntime(25196): at java.lang.reflect.Method.invokeNative(Native Method)
10-24 16:44:02.875: E/AndroidRuntime(25196): at java.lang.reflect.Method.invoke(Method.java:511)
10-24 16:44:02.875: E/AndroidRuntime(25196): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
10-24 16:44:02.875: E/AndroidRuntime(25196): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
10-24 16:44:02.875: E/AndroidRuntime(25196): at dalvik.system.NativeStart.main(Native Method)