Need help with the phone back button to return to Webview

This is my code:

package com.testappmobile; import android.app.Activity; import android.os.Bundle; import android.view.KeyEvent; import android.view.Window; import android.webkit.WebChromeClient; import android.webkit.WebView; import android.webkit.WebViewClient; public class testappmobileActivity extends Activity { final Activity activity = this; private WebView webview; @Override public boolean onKeyDown(int keyCode, KeyEvent event) { // Check if the key event was the BACK key and if there history if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) { webview.goBack(); return true; } // If it wasn't the BACK key or there no web page history, bubble up to the default // system behavior (probably exit the activity) return super.onKeyDown(keyCode, event); } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); this.getWindow().requestFeature(Window.FEATURE_PROGRESS); setContentView(R.layout.main); WebView 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); } }); webView.setWebViewClient(new WebViewClient() { @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { // Handle the error } @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { view.loadUrl(url); return true; } }); webView.loadUrl("http://developer.android.com/index.html"); } } 

Now they are unable to get the equipment return button to work. The application loads fine, like the page and everything else, but as soon as I click the back button, it crashes and then closes. I searched google for the last 3 hours and found only vague answers with very little information or broken links. Google instructions also sucked in as I am new, and they assume you know a certain amount.

Where should I put the code if it is in the wrong place?

Are there any errors?

Hurrah!

+6
source share
2 answers

You created a webView link at the class level, but you never initialized it, so NullPointerException. I made a small change to the onCreate method in your code, analyzed it and make the necessary changes;

 @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 // ... // ... // ... } 

I hope you succeeded. :)

+5
source

You have two different links to WebView. The first local in onCreate that is lost. The second webview element that you use in onKeyDown, but it's still zero.

0
source

Source: https://habr.com/ru/post/897314/


All Articles