所以我试图创建一个显示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)
答案 0 :(得分:3)
更改此行:
WebView myWebView = (WebView) findViewById(R.id.webview);
只是:
myWebView = (WebView) findViewById(R.id.webview);
问题在于,您从未将myWebView
类成员设置为webview引用,而是在onCreate
范围内设置局部变量。