Bazley
Bazley

Reputation: 2847

Android - creating a ProgressDialog without showing it

How do you create a ProgressDialog without showing it? When the WebView starts trying to open a page I want a progress dialog to appear if the page hasn't loaded after two seconds (and disappear if it still hasn't loaded after 30 seconds). The following code is failing because ProgressDialog progress = new ProgressDialog(MainActivity.this);, despite what I've seen in various SO answers, throws a NullPointerException error.

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    // ProgressDialog progress = new ProgressDialog(MainActivity.this); // gives NullPointerException
    ProgressDialog progress;

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

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

        webView.setWebViewClient(new WebViewClient() {
            DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                view.loadUrl(url);
                return false;
            }
            @Override
            public void onPageStarted(WebView view, String url, Bitmap facIcon) {
                timerBeginDialog(2000, progress);
                progress.setCancelable(true);
                timerRemoveDialog(30000, progress);
            }
            @Override
            public void onPageFinished(WebView view, String url) {
                drawer.closeDrawer(GravityCompat.START);
                progress.dismiss();
            }
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
                view.loadUrl("file:///android_asset/www/error.html");
                drawer.openDrawer(GravityCompat.START);
                progress.dismiss();
            }
        });
        .
        .
    }

    public void timerBeginDialog(long time, final ProgressDialog pd){
        new Handler().postDelayed(new Runnable() {
            public void run() {
                pd = ProgressDialog.show(MainActivity.this, "", "Loading...", true); // gives error "Cannot assign a value to final variable pd"
            }
        }, time);
    }

    public void timerRemoveDialog(long time, final ProgressDialog pd){
        new Handler().postDelayed(new Runnable() {
            public void run() {
                pd.dismiss();
            }
        }, time);
    }

Upvotes: 0

Views: 268

Answers (3)

Pasi Matalamäki
Pasi Matalamäki

Reputation: 1853

You cannot pass a variable by reference on Java, so what you need to do is to define the variable within the class, which you apparantly have done, and its name is progress, but withing the method call you're referring to variable 'pd' which is the one it takes as parameter.

You should instead set the value of 'progress' to the new ProgressDialog like this:

progress = ProgressDialog.show(MainActivity.this, "", "Loading...", true);

and then on other method

progress.dismiss();

Upvotes: 0

Onur Çevik
Onur Çevik

Reputation: 1590

Move

ProgressDialog progress = new ProgressDialog(MainActivity.this);

inside onCreate

Upvotes: 1

fedepaol
fedepaol

Reputation: 6862

That's because the context has not been created yet when your initializer runs. Just place the initialization inside onCreate method.

Upvotes: 4

Related Questions