我有一个Web视图,其中后退按钮不起作用,并导致nullpointer异常。我正在使用一个活动,并检查要加载的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)


任何帮助,将不胜感激。

谢谢,

最佳答案

WebView myWebview = (WebView) findViewById(R.id.webEngine);

应该

myWebview = (WebView) findViewById(R.id.webEngine);

您在if块中声明一个新的WebView,然后尝试在onBackPressed()中引用类变量myWebView(从未实例化)。如果要进一步确保始终使用类变量,也可以在所有情况下都使用this.myWebView。

关于android - Android Webview-后退按钮不起作用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/17410459/

10-09 13:05