Android webview:方法setProgress(int)未定义类型new WebChromeClient(){}

时间:2013-02-07 00:27:57

标签: java android android-webview

我正在尝试设置加载圈以显示在我的Android网络视图应用中。自从我开始以来,我取得了很大的进步。现在我收到了行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();
    }


}

1 个答案:

答案 0 :(得分:3)

改为

MainActivity.this.setProgress(progress * 100);

或只是

setProgress(progress * 100);

因此,您明确地引用父类实例。在this内部执行WebChromeClient时,会引用 类的实例,因此会出现编译时错误。

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