Gibson
Gibson

Reputation: 810

Android progressBar random crash with nullPointerException, can't understand why

Here's one of the tabs I have that loads a page.

package realstrat.cfostudio.magazineapp;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
import realstrat.cfostudio.magazineapp.R;

public class TabActivity3 extends Activity {
    WebView mWebView;
    private ProgressDialog progressBar;

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

        mWebView = (WebView) findViewById(R.id.webview);
        mWebView.getSettings().setJavaScriptEnabled(true);
        mWebView.getSettings().setPluginsEnabled(true);
        mWebView.loadUrl("--company URL--");

        mWebView.setWebViewClient(new FirstTabWebViewClient());        
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
          savedInstanceState.putBoolean("OverviewMode", mWebView.getSettings().getLoadWithOverviewMode());
          mWebView.saveState(savedInstanceState);
          super.onSaveInstanceState(savedInstanceState);
    }

    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
          super.onRestoreInstanceState(savedInstanceState);

          ((WebView)findViewById(R.id.webview1)).restoreState(savedInstanceState);
          if (savedInstanceState.getBoolean("OverviewMode") == false) {
              ((WebView)findViewById(R.id.webpageview)).getSettings().setLoadWithOverviewMode(false);
              ((WebView)findViewById(R.id.webpageview)).getSettings().setUseWideViewPort(false);                
          }
          else {
              ((WebView)findViewById(R.id.webpageview)).getSettings().setLoadWithOverviewMode(true);
              ((WebView)findViewById(R.id.webpageview)).getSettings().setUseWideViewPort(true);
          }          
          return;
    }

    private class FirstTabWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
          // YouTube video link
        if (url.startsWith("vnd.youtube"))
            {
            startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            return (true);
            }
        if (url.endsWith("-m.html")){
            mWebView.getSettings().setLoadWithOverviewMode(false);
            mWebView.getSettings().setUseWideViewPort(false);               
        }
        else {
            mWebView.getSettings().setLoadWithOverviewMode(true);
            mWebView.getSettings().setUseWideViewPort(true);
        }

          view.loadUrl(url);
          return true;
        }

        public void onPageStarted(WebView view, String url, Bitmap favicon){
            progressBar = ProgressDialog.show(TabActivity3.this, "", "Loading...", true);

        }

        public void onPageFinished(WebView view, String url) {
            progressBar.hide();

            if (url.endsWith("-m.html")){
                mWebView.getSettings().setLoadWithOverviewMode(false);
                mWebView.getSettings().setUseWideViewPort(false);               
            }
            else {
                mWebView.getSettings().setLoadWithOverviewMode(true);
                mWebView.getSettings().setUseWideViewPort(true);
            }
            return;
        }
        public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
            Context context = getApplicationContext();
            CharSequence text = "Desc: " + description;
            int duration = Toast.LENGTH_SHORT;
            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
            return;
        }
    }
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if ((keyCode == KeyEvent.KEYCODE_BACK) && mWebView.canGoBack()) {
            mWebView.goBack();
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }
}

Once in a while it will crash with a nullPointerException on the line progressBar.hide() under onPageFinished(). That doesn't make any sense since onPageStarted() starts the progressBar, and onPageStarted always comes before onPageFinished(). Why is this?

This only happens like, once in 10 times or something, which is really confusing to me.

It usually happens (always?) when the activity is being started for the first time.

Upvotes: 0

Views: 1488

Answers (3)

Nalan Dayarathna
Nalan Dayarathna

Reputation: 1

Try to load progress bar as singleton object. If you create anther progress bar object before hide first one then second progress bar will crash in hide().

if(_progressBar == null)
    _progressBar = new ProgressDialog(this);

Upvotes: 0

Ahmed Salem
Ahmed Salem

Reputation: 3969

try this

if(progressBar!=null)
       progressBar.hide();

Upvotes: 4

Segfault
Segfault

Reputation: 8290

Maybe the Activity has been restarted in between the two callbacks? Try rotating the phone while the progressBar is shown to see what the results are.

Upvotes: 0

Related Questions