简体   繁体   中英

Android Add indeterminate progress bar to webview when loading url

Hi I'm trying to implement a progress bar to my webview that pops up while waiting for the webview to load. I've seen a few ways of doing it but I'm not sure which is the best way to go about doing it.

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

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

</RelativeLayout>

I create the progress bar programmatically!

Activity:

    public class MapActivity extends MenuActivity {

private final static String TAG = "HearingTest";
private WebView webView;
private String urlString;
private ProgressDialog progressDialog;
private Context context;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.map_layout);

    urlString = getString(R.string.location_url);

    WebViewSettings();

    LoadWebPage(urlString);

    AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

        @Override
        protected void onPreExecute() {
            progressDialog = new ProgressDialog(context);
            progressDialog.setTitle("Loading...");
            progressDialog.setMessage("Please wait.");
            //progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            try {
                //Do something...
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            if (progressDialog!=null) {
                progressDialog.dismiss();
            }
        }
    };

    task.execute((Void[])null);
}

public void LoadWebPage(String url){

    try {
        webView.loadUrl(url);
        Log.d(TAG, "URL" + url + "connected");
    } 
    catch (Exception e) {
            Log.d(TAG, "URL: " + url + " couldn't connect.");
    }
}

public void WebViewSettings(){

    webView = (WebView) findViewById(R.id.webview);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setLoadWithOverviewMode(true);

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            if (Uri.parse(url).getHost().equals(urlString)) {
                // This is my web site, so do not override; let my WebView load the page
                return false;
            }
            // Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
            Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
            startActivity(intent);
            return true;
        }

        @Override
        public void onReceivedSslError (WebView view, SslErrorHandler handler, SslError error) {
             handler.proceed();
        }
    });
}

    protected void DisplayProgressBar(){

    }
}

The problem I've got is I don't really know what the best way to go about doing it is. The way I'm currently doing it I get a null pointer exception when I create a new instance of the progress bar, I can't figure out why! Plus I don't really know if I'm doing it the best way as I have seen another way to do it using thread.

If anyone could help me solve my issue or tell be a better solution that would be greatly appreciated.

The context is null and that's probably why you're getting the NullPointerException

Initialize context with context = MapActivity.this; in onCreate

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