如何修复“找不到符号变量MyWebViewClient”

时间:2019-01-16 11:36:49

标签: java android

我得到error: cannot find symbol variable MyWebViewClient

public class MainActivity extends AppCompatActivity {
    WebView myWebView = (WebView) findViewById(R.id.webview);

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

        myWebView.loadUrl("https://*********.com"); //Just removed for security
        WebSettings ws = myWebView.getSettings();
        ws.setJavaScriptEnabled(true);


        WebView myWebView = (WebView) findViewById(R.id.webview);
        myWebView.setWebViewClient(MyWebViewClient);


        WebView webView = (WebView) findViewById(R.id.webview);
        webView.addJavascriptInterface(new WebAppInterface(this), "Android");
    }

    @Override
    public void onBackPressed() {
        if (myWebView.canGoBack()) {
            myWebView.goBack();
        }
        else{
            super.onBackPressed();
        }
    }
}

和我的MyWebViewClient

public class MyWebViewClient extends WebViewClient {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        if (Uri.parse(url).getHost().equals("https://*********.com")) {
            return false;
        }
        // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        return true;
    }
}

(按照Android-studio的说明导入了所有必要的头文件) 但是我得到error: cannot find symbol variable MyWebViewClient

请有人帮助我。

1 个答案:

答案 0 :(得分:0)

您似乎正在尝试将MyWebViewClient类作为该行的实例传递:

myWebView.setWebViewClient(MyWebViewClient);

也许尝试这样的事情:

myWebView.setWebViewClient(new MyWebViewClient());

MyWebViewClient myWebviewClientInstance = new MyWebViewClient();
myWebView.setWebViewClient(myWebviewClientInstance);