java - 安卓网页 View : The method setProgress(int) is undefined for the type new WebChromeClient(){}

标签 java android android-webview

我正在尝试设置一个加载圆圈以显示在我的 Android Web View 应用程序中。自从我开始以来,我已经取得了很大的进步。现在我在 setProgress 行中遇到错误。

错误信息是:

未针对 new WebChromeClient() 类型定义 setProgress(int) 方法{}

import android.os.Bundle;
import android.app.Activity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.webkit.WebChromeClient;

public class MainActivity extends Activity 
{   
    //Configure the webview setup in the xml layout
    public WebView myWebView;


     @Override
        public void onCreate(Bundle savedInstanceState) 
        {
            super.onCreate(savedInstanceState);
            //Remove title bar as we already have it in the web app
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);

            getWindow().requestFeature(Window.FEATURE_PROGRESS);
            //Point to the content view defined in XML
            setContentView(R.layout.activity_main);
            myWebView = (WebView) findViewById(R.id.webview);
            myWebView.getSettings().setJavaScriptEnabled(true);
            myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);


            WebSettings webSettings = myWebView.getSettings();
            //Yes, we want javascript, pls.

            webSettings.setJavaScriptEnabled(true);
            //Make sure links in the webview is handled by the webview and not sent to a full browser
            myWebView.setWebViewClient(new WebViewClient());
            //And let the fun begin
            myWebView.loadUrl("http://www.google.com");

            myWebView.setWebChromeClient(new WebChromeClient() {
                 public void onProgressChanged(WebView view, int progress) {
                  // Activities and WebViews measure progress with different scales.
                  // The progress meter will automatically disappear when we reach 100%
                  this.setProgress(progress * 100);

                 }});

        }



    @Override
    public void onBackPressed(){
        myWebView.goBack();
    }


}

最佳答案

改为这样做

MainActivity.this.setProgress(progress * 100);

或者只是

setProgress(progress * 100);

以便您显式引用父类实例。在 WebChromeClient 内部执行 this 操作会引用 那个 类的实例,因此您会收到编译时错误。

由于您正在使用 WebView,请参阅文档以获取有关 WebView 的更多信息。以及其他所有相关内容(包括客户端)。

关于java - 安卓网页 View : The method setProgress(int) is undefined for the type new WebChromeClient(){},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14741525/

相关文章:

java - 无法从 Genymotion 模拟器连接到本地 gae 端点

java - 使用 rest-assured 作为通用的 http 客户端

android - splitdexfile 导致崩溃 : Facebook SDK Parcelable encounteredClassNotFoundException reading a Serializable object

安卓浏览器打不开网页

Android Webview - navigator.geolocation.getCurrentPosition 不工作

android - 对 WebView 中图像的相对引用

Java使用PDFBox图像转换获取空白图像

java - 使用 RabbitMq MessageListenerContainer 的 Reactor Flux 超时

java - Android 模拟器因 Db 崩溃(找不到错误 - 新手)

android - 无法在 Android Studio 中运行单元测试