找不到符号变量MyWebViewClient

时间:2019-04-16 17:44:51

标签: java android android-webview

我正在尝试使用Android Studio和Java创建一个webview android应用程序。我可以使用它,但是后来意识到我还需要在网站上的页面之间导航。在查看https://developer.android.com/guide/webapps/webview时,我发现必须添加以下代码行:myWebView.setWebViewClient(MyWebViewClient);。但是,这行代码给了我一个错误。

我曾尝试清洁和重建该项目。我尝试导入import android.webkit.WebViewClient;。我已经尝试过MyWebViewClient myWebviewClientInstance = new MyWebViewClient(); myWebView.setWebViewClient(myWebviewClientInstance);myWebView.setWebViewClient(new MyWebViewClient());。但是,所有这些似乎都不起作用。

这是我的代码: activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

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

</android.support.constraint.ConstraintLayout>

还有我的MainActivity.java文件(在此文件中是错误):

package com.rosaliewessels.mywebview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends AppCompatActivity {

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


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

       myWebView.setWebViewClient(MyWebViewClient); \\ERROR on MyWebViewClient



        myWebView.loadUrl("https://www.mijnmedicijn.nl");



    }
}

和AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.rosaliewessels.mywebview">

    <uses-permission android:name="android.permission.INTERNET" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

如何解决错误并能够在网站上的页面之间导航?我真的是Android开发的新手,我们将不胜感激。

1 个答案:

答案 0 :(得分:1)

问题出在这里-

myWebView.setWebViewClient(MyWebViewClient); MyWebViewClient上的\ ERROR

无论何时使用setWebViewClient方法,我们都需要创建其内部类,只需像这样创建它-

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    WebView myWebView = (WebView) findViewById(R.id.webview);

//下一行将更改

    **myWebView.setWebViewClient(new MyWebViewClient());**
    myWebView.loadUrl("https://www.mijnmedicijn.nl");
   }

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

//要在移动Web视图中执行单击操作,可以启用如下Java脚本

 **myWebView.getSettings().setJavaScriptEnabled(true);**

希望对您有帮助,快乐编码:)

相关问题