android 如何修复此错误;错误:方法不重写或实现父类型中的方法?

e1xvtsh3  于 2022-11-03  发布在  Android
关注(0)|答案(2)|浏览(229)

我有一个工作的Android webview应用程序,我用Android Studio,我需要让它打开外部包含“_blank”的链接.我添加了下面的代码

@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”的第一行显示了下面的错误

> 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

以下是完整的“MainActivity”代码

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);
    }

}

我已经尝试了几个位置的代码,但错误仍然存在。我想摆脱错误,并实现的原因,我添加了代码;在webview应用程序外打开目标”_blank”。

9bfwbjaz

9bfwbjaz1#

转到建立gradle(项目)或设置
在前面插入此行

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

在插入这一行之前插入这一行-〉imagex 1c 0d1x和#同步项目#android_Studio

6tr1vspr

6tr1vspr2#

该错误告诉您问题所在。WebViewClient没有任何名为onCreateWindow的方法。

WebChromeClient确实如此

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可以同时拥有这两个版本,因此您只需创建自己的WebChromeClient版本并将其添加到其中。

// 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());

相关问题