1

I have an Activity with a WeView that load an url, and I want to display a little waiting dialog during the load of the site, so I have tried this:

private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this);

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();
        StrictMode.setThreadPolicy(policy);
        setContentView(R.layout.web_view_activity);

        WebView wv;
        wv = (WebView) findViewById(R.id.areaWebSolver);
        wv.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return true;
            }
            @Override
        public void onPageFinished(WebView view, String url) {                  
            if (dialog.isShowing()) {
                dialog.dismiss();
            }
        }

        });
        dialog.setMessage("Loading..Please wait.");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        wv.loadUrl(url);

        WebSettings webSettings = wv.getSettings();
        webSettings.setJavaScriptEnabled(true);

    }

Unfortunately doesn't works and the app crashes with a source not found... If I try to remove the Progress dialog code the activity works. What's wrong? How could I fix this?

AndreaF
  • 11,975
  • 27
  • 102
  • 168

4 Answers4

0

You can set the ThreadPolicy for whatever reasons after setContentView() method, and your progress dialog will never get Context that is your statement below will not load the activity context at all.

private ProgressDialog dialog = new ProgressDialog(this);

Find the pseudo code below which works for me.

private ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.web_view_activity);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading..Please wait.");
    dialog.setCanceledOnTouchOutside(false);
    dialog.show();

    WebView wv = (WebView) findViewById(R.id.areaWebSolver);
    wv.setWebViewClient(new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, String url) {                  
           if (dialog.isShowing()) {
              dialog.dismiss();
           }
        }
        ...
    }

The source not found may be due to some other external libraries which you've linked and its not available in your IDE's classpath. READ HERE for more info.

Community
  • 1
  • 1
Lalith B
  • 11,843
  • 6
  • 29
  • 47
0

This

     private ProgressDialog dialog = new ProgressDialog(MyNameActivity.this);
     //This will give you NullPointerException. 
     // You can use the activity context after you set the content to the activity.    

Should be

     private ProgressDialog dialog;   
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
            .permitAll().build();
    StrictMode.setThreadPolicy(policy);
    setContentView(R.layout.web_view_activity); 
    dialog = new ProgressDialog(MyNameActivity.this);
    .... 
    }
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
0

You should be used Progress Bar in your webview activity

// Just find 
 progressBar = (ProgressBar) findViewById(R.id.progressBar1);
//in onCreate Method of activity and than after 

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

xml:

<?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/webview01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </WebView>

    <ProgressBar
        android:id="@+id/progressBar1"
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>
Bhoomika Brahmbhatt
  • 7,404
  • 3
  • 29
  • 44
Ketan Mehta
  • 272
  • 1
  • 12
0

I think this will help you.

private ProgressDialog dialog;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view_activity);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
        .permitAll().build();
StrictMode.setThreadPolicy(policy);



WebView wv = (WebView) findViewById(R.id.areaWebSolver);
wv.setWebViewClient(new WebViewClient() {
    @Override
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
    dialog = new ProgressDialog(this);
    dialog.setMessage("Loading..Please wait.");
    dialog.setCanceledOnTouchOutside(false);

    if(!dialog.showing()){
      view.loadUrl(url);
      dialog.show();
        return true;
    }

    }
    @Override
    public void onPageFinished(WebView view, String url) {                  
       if (dialog.isShowing()) {
          dialog.dismiss();
       }
    }
    ...
}
Shani Goriwal
  • 2,111
  • 1
  • 18
  • 30