为什么我的应用程序在启动时崩溃?

时间:2013-09-03 16:40:22

标签: java android android-webview android-progressbar

我无法将webview应用程序中的进度条更改为circle.the应用程序在启动时崩溃。这是代码

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        WebSettings websettings = webview.getSettings();
        websettings.setJavaScriptEnabled(true);

        getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS); // Request progress circle
        setProgressBarIndeterminateVisibility(true); // Show progress circle

    //  webview.setWebViewClient(new WebViewClient());

        final Activity activity = this;

        webview.setWebChromeClient(new WebChromeClient(){

                public void onProgressChanged(WebView view, int progress) {
                    activity.setTitle("Loading...");
                    activity.setProgress(progress * 100);
                    if(progress == 100)
                        setProgressBarIndeterminateVisibility(false); // Hide progress circle when page loaded
                    activity.setTitle("Title");
                }
            });
        if (savedInstanceState == null)
        {
            webview.loadUrl("http://www.proboards.com/");
        }


    }

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

我在你的代码中看到了一些缺陷。

As far as I know, the ProgressBar value should be less than or equal to 100.
You are setting it to (progress * 100)

Also, once progress == 100, you hide the progress bar,
this will take place when progress = 1. Then you hide the progress bar.

<强> Hope this helps.

    private ProgressBar mProgress;
    private int mProgressStatus = 0;

    mProgress = (ProgressBar) findViewById(R.id.progress_bar);

    // Start lengthy operation in a background thread
         new Thread(new Runnable() {
             public void run() {
                 while (mProgressStatus < 100) {
                     mProgressStatus = doWork();

                     // Update the progress bar
                     mHandler.post(new Runnable() {
                         public void run() {
                             mProgress.setProgress(mProgressStatus);
                         }
                     });
                 }
             }
         }).start();

答案 1 :(得分:0)

您的应用崩溃了,因为这一行:

Activity.getWindow().requestFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
在调用 Activity.setContentView(...);

之前,

需要被称为

之前您正在调用setContentView(...),这就是您的应用崩溃的原因。

这就是为什么,从Android-Developer页面:

  

http://developer.android.com/reference/android/view/Window.html#requestFeature%28int%29