我有一个 web 视图,其中后退按钮不起作用并导致空指针异常。我正在使用单个活动并检查要加载的 url 的意图附加内容。打开链接的所有功能都正常,只是后退按钮不会返回到 webview 中的上一页,而是会使活动崩溃。
已编辑:将代码块更新为以下建议。仍然给出空指针异常
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class WebUrl extends Activity {
WebView myWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);
Bundle extras = getIntent().getExtras();
if(extras != null) {
String link = extras.getString("url");
WebView myWebview = (WebView) findViewById(R.id.webEngine);
myWebview.getSettings().setJavaScriptEnabled(true);
myWebview.getSettings().setSupportZoom(true) ;
myWebview.getSettings().setUseWideViewPort(true) ;
myWebview.getSettings().setLoadWithOverviewMode(true);
myWebview.getSettings().setBuiltInZoomControls(true);
myWebview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
myWebview.setScrollbarFadingEnabled(false);
// Load the URLs inside the WebView, not in the external web browser
myWebview.setWebViewClient(new WebViewClient());
if (savedInstanceState == null) {
// Load a page
myWebview.loadUrl(link);
}
}
}
@Override
public void onBackPressed() {
if(myWebview.canGoBack() == true) {
myWebview.goBack();
} else {
WebUrl.super.onBackPressed(); //Replace MyActivity With the name of your activity.
}
}
}
Logcat 错误:
07-01 11:54:19.817: E/AndroidRuntime(10079): FATAL EXCEPTION: main
07-01 11:54:19.817: E/AndroidRuntime(10079): java.lang.NullPointerException
07-01 11:54:19.817: E/AndroidRuntime(10079): at com.app.browser.WebUrl.onBackPressed(WebUrl.java:70)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.app.Activity.onKeyUp(Activity.java:2163)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.view.KeyEvent.dispatch(KeyEvent.java:2651)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.app.Activity.dispatchKeyEvent(Activity.java:2393)
07-01 11:54:19.817: E/AndroidRuntime(10079): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchKeyEvent(PhoneWindow.java:1853)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3683)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3653)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:2893)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.os.Looper.loop(Looper.java:137)
07-01 11:54:19.817: E/AndroidRuntime(10079): at android.app.ActivityThread.main(ActivityThread.java:5059)
07-01 11:54:19.817: E/AndroidRuntime(10079): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 11:54:19.817: E/AndroidRuntime(10079): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 11:54:19.817: E/AndroidRuntime(10079): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
07-01 11:54:19.817: E/AndroidRuntime(10079): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
07-01 11:54:19.817: E/AndroidRuntime(10079): at dalvik.system.NativeStart.main(Native Method)
任何帮助,将不胜感激。
谢谢,