简体   繁体   中英

Loading Dialog Box for Webview

This things sending me crazy. My app loads webview and shows a loading dialog on the initial load. I want the loading dialog to appear each time a link is clicked or each time webview is loading. This is not happening.

Eclipse tells me onPageStarted() is not used locally, although onPageFinished() works fine!?

Can anyone see what's going wrong, I've pasted all my activity below:

   package com.jeh.myapp;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MyActivity extends Activity {

    WebView myWebView;
    public static final String TAG = "Main";
    public ProgressDialog progressBar; 
    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);
        //Point to the content view defined in XML
        setContentView(R.layout.main);
        //Configure the webview setup in the xml layout
        myWebView = (WebView) findViewById(R.id.webview);
        WebSettings webSettings = myWebView.getSettings();
        //Yes, we want javascript, pls.
        webSettings.setJavaScriptEnabled(true);

        progressBar = ProgressDialog.show(MyActivity.this, "Diag Title", "Loading...");


        //Make sure links in the webview is handled by the webview and not sent to a full browser
        myWebView.setWebViewClient(new WebViewClient() {


            //this bit causes problems, if I add @Override here it says to remove, where as the current code marks onPageStarted yellow and says it's not used locally!? - yet onPageFinsihed() below works fine?
            public void onPageStarted(WebView view, String url, Bitmap favicon) {

                progressBar.show();
            }


             public void onPageFinished(WebView view, String url) {
                 Log.i(TAG, "Finished loading URL: " +url);
                 if (progressBar.isShowing()) {
                     progressBar.dismiss();
                 }
             }

        });
        //Load URL:
        myWebView.loadUrl("http://www.google.com");



    }

Easy. There are no

void onPageStarted(WebView view, String url)

method in a WebViewClient , simply use :

@Override
void onPageStarted(WebView view, String url, Bitmap favicon)

These errors are easy to make and difficult to find. You should use @Override keyword wherever you know you're overriding a method (which would have flagged an error in your case).

Try to modify your source this way:

//Make sure links in the webview is handled by the webview and not sent to a full browser
myWebView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        progressBar.show();
    }

    @Override
     public void onPageFinished(WebView view, String url) {
         Log.i(TAG, "Finished loading URL: " +url);
         if (progressBar.isShowing()) {
             progressBar.dismiss();
         }
     }

});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM