Home > other >  onBackpressed() error - error: cannot find symbol super.onBackPressed();
onBackpressed() error - error: cannot find symbol super.onBackPressed();

Time:12-16

Can you help me solving me this error. I try to make a webView app for Android, but when I run it, I receive this error:

error: cannot find symbol super.onBackPressed(); ^ symbol: method onBackPressed()

Below is my MainActivity.java code:

package com.example.balkanconta;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

    private WebView myWebview;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        myWebview=(WebView)findViewById(R.id.webview);
        myWebview.setWebViewClient(new WebViewClient());
        myWebview.loadUrl("https://example.com/");
        WebSettings webSettings=myWebview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        this.myWebview = (WebView) findViewById(R.id.webview);
    }

    public class myWebClient extends WebViewClient implements com.example.balkanconta.myWebClient {
        @Override
        public void onPageStarted(WebView view,
                                  String url,
                                  Bitmap favicon) {
            super.onPageStarted(view,url,favicon);
        }
        @Override
        public boolean shouldOverrideUrlLoading(WebView view,
                                                String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onBackPressed() {
            // now you don't need findViewById because you can access field prepared in onCreate
            if (myWebview.canGoBack()) {
                myWebview.goBack();
            } else {
                super.onBackPressed();
            }
        }

    }
}

CodePudding user response:

WebViewClient doesn't have a method onBackPressed, so it cannot be overridden. You should move the method from myWebClient directly to your MainActivity.

  • Related