WebView中加载URL时的进度条

时间:2014-10-17 12:41:07

标签: android webview progress-bar

我有以下代码来显示在WebView中加载URL时加载进度条。但是代码行:

webView.setWebViewClient(new MyWebViewClient());

有错误:

The constructor MyWebViewClient() is undefined

这还需要什么工作?

WebViewActivity:

public class WebViewActivity extends Activity {

private WebView webView;


public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.webview);

ProgressBar pb = new ProgressBar(WebViewActivity.this);

webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient(pb));
webView.loadUrl("http://www.google.com");

}


@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN){
    switch(keyCode)
    {
    case KeyEvent.KEYCODE_BACK:
        if(webView.canGoBack()){
            webView.goBack();
        }else{
            finish();
        }
        return true;
    }

}
return super.onKeyDown(keyCode, event);
}
}

MyWebViewActivity:

class MyWebViewClient extends WebViewClient {

private ProgressBar progressBar;

 public MyWebViewClient(ProgressBar pb) {
   this.progressBar=pb;
   pb.setVisibility(View.VISIBLE);}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
    view.loadUrl(url);
    return super.shouldOverrideUrlLoading(view, url);
}

@Override
public void onPageFinished(WebView view, String url) {
    // TODO Auto-generated method stub
    super.onPageFinished(view, url);
    progressBar.setVisibility(View.GONE);
}

}

崩溃日志:

10-17 20:18:35.774: E/AndroidRuntime(10492): FATAL EXCEPTION: main
10-17 20:18:35.774: E/AndroidRuntime(10492): java.lang.RuntimeException: Unable to start activity     ComponentInfo{app.myapp/app.myapp.WebViewActivity}: java.lang.NullPointerException
10-17 20:18:35.774: E/AndroidRuntime(10492):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.ActivityThread.access$600(ActivityThread.java:139)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1262)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.os.Looper.loop(Looper.java:156)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.ActivityThread.main(ActivityThread.java:4987)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at java.lang.reflect.Method.invokeNative(Native Method)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at java.lang.reflect.Method.invoke(Method.java:511)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at dalvik.system.NativeStart.main(Native Method)
10-17 20:18:35.774: E/AndroidRuntime(10492): Caused by: java.lang.NullPointerException
10-17 20:18:35.774: E/AndroidRuntime(10492):    at app.myapp.MyWebViewClient.<init>(MyWebViewClient.java:18)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at app.myapp.WebViewActivity.onCreate(WebViewActivity.java:23)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.Activity.performCreate(Activity.java:4538)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
10-17 20:18:35.774: E/AndroidRuntime(10492):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
10-17 20:18:35.774: E/AndroidRuntime(10492):    ... 11 more

4 个答案:

答案 0 :(得分:1)

The constructor MyWebViewClient() is undefined当使用的构造函数在Class中不可用时会出现此错误。我可以看到你只有一个构造函数public MyWebViewClient(ProgressBar progressBar)。在MyWebViewClient班级

因此,如果不将MyWebViewClient实例传递给构造函数,则无法创建ProgressBar类的实例。

答案 1 :(得分:1)

public MyWebViewClient(ProgressBar progressBar) {
    this.progressBar=progressBar;
    progressBar.setVisibility(View.VISIBLE);
}

构造函数有一个ProgressBar作为参数,如上所示。您必须将进度条作为参数传递。

答案 2 :(得分:0)

public class google extends Activity {
    WebView video_player_view;
    ProgressDialog pd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.google);
        video_player_view=(WebView)findViewById(R.id.webView1);
         video_player_view.getSettings().setJavaScriptEnabled(true);
           video_player_view.getSettings().setLoadsImagesAutomatically(true);
           video_player_view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
           video_player_view.setWebViewClient(new MyBrowser());
              video_player_view.loadUrl("YOUR URL");
              pd= new ProgressDialog(this);
              pd.setTitle("Webpage loading");
              pd.setCancelable(false);
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(video_player_view.canGoBack()){
                    video_player_view.goBack();
                }else{
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

     private class MyBrowser extends WebViewClient {
          @Override
          public boolean shouldOverrideUrlLoading(WebView view, String url) {
              view.loadUrl(url); 
              view.addJavascriptInterface(new Object()
              {
                  @JavascriptInterface
                public void performClick() throws Exception
                {
                    Log.d("LOGIN::", "Clicked");
                     Toast.makeText(google.this, "Login clicked", Toast.LENGTH_LONG).show();
                }
              }, "login");
             return true;
          }

          @Override
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // TODO Auto-generated method stub

              System.out.println("started");
              pd.show();
            super.onPageStarted(view, url, favicon);



        }



          @Override
        public void onPageFinished(WebView view, String url) {
            // TODO Auto-generated method stub
              pd.dismiss();
              System.out.println("ends");
            super.onPageFinished(view, url);



        }
       }    

答案 3 :(得分:0)

我最近实现了这个,它对我很有用:

private WebView socialWebView;
private ProgressDialog loadPageDialog;


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

    socialWebView = (WebView)findViewById(R.id.socialWebView);
    socialWebView.getSettings().setJavaScriptEnabled(true);
}


private void initData() {
    loadPageDialog = new ProgressDialog(this);
    loadPageDialog.setTitle("Loading...");
    loadPageDialog.setMessage("Please wait...");
    loadPageDialog.setCancelable(true);
    loadPageDialog.setCanceledOnTouchOutside(true);
    if(loadPageDialog != null && !loadPageDialog.isShowing()) {
        loadPageDialog.show();
    }

    socialWebView.setWebViewClient(new WebViewClient()
    {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url)
        {
            return false;
        }

        @Override
        public void onPageStarted(WebView view, String url, Bitmap facIcon) {
        }

        @Override
        public void onPageFinished(WebView view, String url) {
            if(loadPageDialog != null && loadPageDialog.isShowing()) {
                loadPageDialog.dismiss();
            }
        }

    });

}

页面加载后,对话框将被忽略,希望这是您正在寻找的