Home > OS >  How do I fix this error; error: method does not override or implement a method from a supertype?
How do I fix this error; error: method does not override or implement a method from a supertype?

Time:11-02

I have a working Android webview App I made using Android Studio, I needed to make it open links that contains "_blank" externally. I added the code below

 @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
        {
            WebView.HitTestResult result = view.getHitTestResult();
            String data = result.getExtra();
            Context context = view.getContext();
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
            context.startActivity(browserIntent);
            return false;
        }

But the first line of the code "@Override" shows the error below

> Task :app:compileDebugJavaWithJavac FAILED
...\MainActivity.java:58: error: method does not override or implement a method from a supertype
        @Override
        ^
Note: ...\MainActivity.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
1 error

Below is the Full "MainActivity" Code

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.net.http.SslError;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.SslErrorHandler;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity implements Main_Activity, Main__Activity {

    WebView webView;

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

        webView = findViewById(R.id.web);

        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setSupportZoom(false);
        webView.getSettings().setDomStorageEnabled(true);
        webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
        webView.getSettings().setSupportMultipleWindows(true);
        webView.getSettings().setUseWideViewPort(false);
        webView.setWebViewClient(new WebViewClient() {

        });

        webView.loadUrl("file:///android_asset/app.html");
    }

    public class myWebViewClient extends WebViewClient{
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }

        @Override
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            webView.loadUrl("file:///android_asset/error.html");
        }

        @Override
        public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
            super.onReceivedSslError(view, handler, error);
            handler.cancel();
        }
        @Override
        public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
        {
            WebView.HitTestResult result = view.getHitTestResult();
            String data = result.getExtra();
            Context context = view.getContext();
            Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
            context.startActivity(browserIntent);
            return false;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {

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

}

I've tried several locations for the code but the error persists. I want to get the error off and achieve the reason why I added the code; to open target"_blank" outside the webview app.

CodePudding user response:

Go to build gradle (project) or setting

insert this line before

 repositories {
        google()
        maven Central()
} // 

this Line before insert This -> imageenter image description here and #synk project #android_studio

CodePudding user response:

The error tells you what is the problem. WebViewClient does not have any method called onCreateWindow.

WebChromeClient does.

public class MyChromeViewClient extends WebChromeClient {
    @Override
    public boolean onCreateWindow(WebView view, boolean dialog, boolean userGesture, android.os.Message resultMsg)
    {
        WebView.HitTestResult result = view.getHitTestResult();
        String data = result.getExtra();
        Context context = view.getContext();
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(data));
        context.startActivity(browserIntent);
        return false;
    }
}

WebView can have both of them so you just have to create your own version of the WebChromeClient and add it to it.

// Also BTW you are not using your implementation - it should be new myWebViewClient()
webView.setWebViewClient(new WebViewClient());
//webView.setWebViewClient(new myWebViewClient());
webView.setWebChromeClient(new MyChromeViewClient());
  • Related