Need assistance using phones back button to go back in Webview

你离开我真会死。 提交于 2019-12-05 07:40:56

You have created a webView reference at class level but never initialized it thats why NullPointerException. I've made a small change in the onCreate method in your code, analyze it and make the necessary change(s);

@Override
public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
                setContentView(R.layout.main);

                // Don't create another webview reference here,
                // just use the one you declared at class level.
                webView = (WebView) findViewById(R.id.webView);
                webView.getSettings().setJavaScriptEnabled(true);

                webView.setWebChromeClient(new WebChromeClient() {
                    public void onProgressChanged(WebView view, int progress)
                    {
                        activity.setTitle("Loading...");
                        activity.setProgress(progress * 100);

                        if(progress == 100)
                            activity.setTitle(R.string.app_name);
                    }
                });

          // rest of code same
          // ...
          // ...
          // ...
}

Hope you got it. :)

You have two different references to WebView. First local in onCreate, that is lost. Second webview member that you use in onKeyDown, but that is null all the time.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!