My application is done in webview and by clicking back with the android phone button it shows the msg Auncher stopped

Asked

Viewed 138 times

-1

  1. This is my code Mainactivity.java when running on a device I click the back button simply appears the message Launcher stopped

I really need to understand what is missing in the code my main concept of error is everywhere in (keycode == Keyevent.KEYCODE_BACK && webView.canGoBack())

public class MainActivity extends AppCompatActivity {

    private Object WebResourceRequest;
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final ProgressBar progressBar = findViewById(R.id.progress);
        progressBar.setVisibility(View.INVISIBLE);

        WebView webView = findViewById(R.id.webview);
        webView.loadUrl("https://oolze.com.br/");
        webView.getSettings().setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient(){
            @Override
            public void onPageStarted(WebView view, String url, Bitmap favicon) {
                super.onPageStarted(view, url, favicon);
                progressBar.setVisibility(View.VISIBLE); // mostra a progress
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                super.onPageFinished(view, url);
                progressBar.setVisibility(View.INVISIBLE); // esconde a progress
            }
        });

    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK && webView.canGoBack()) {
            webView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
 }

My activity_main.xml Here’s the Activity xml where the webview and progress bar are

  <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">

        <WebView
            android:id="@+id/webview"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <ProgressBar
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"/>

    </FrameLayout>

1 answer

0


You are creating and initiating a local variable called webView not the instance variable of the same name. You should initialize the instance variable.

In other words, change that line:

WebView webView = findViewById(R.id.webview);

for that:

webView = findViewById(R.id.webview);

Another thing, I don’t know if the effect is the same, but instead of overwriting onKeyDown() I suggest overwriting onBackPressed(), it was meant to be overwritten in cases like this.

  • Very obg, gave certoooooo !!! was breaking his head because of a line of code kkkk

  • If the answer served, mark it as accepted. See on tour how to do.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.