Android progressBar随机崩溃与nullPointerException,无法理解为什么

时间:2011-10-21 19:47:43

标签: android webview progress-bar progressdialog

这是我加载页面的标签之一。

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);
    }
}

偶尔会在onPageFinished()下的progressBar.hide()行中因nullPointerException而崩溃。这没有任何意义,因为onPageStarted()启动了progressBar,onPageStarted总是在onPageFinished()之前。这是为什么?

这种情况只发生在10次或者其他事情中,这对我来说真的很困惑。

第一次启动活动时,通常会发生(总是?)。

3 个答案:

答案 0 :(得分:4)

试试这个

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

答案 1 :(得分:0)

可能在两个回调之间重新启动了Activity?在显示progressBar时尝试旋转手机以查看结果。

答案 2 :(得分:0)

尝试将进度条加载为单例对象。如果在隐藏第一个之前创建另一个进度条对象,则第二个进度条将在hide()中崩溃。

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