3

我尝试使用此解决方案 Android WebView 进度条

这给了我

    final ProgressBar Pbar;
    Pbar = (ProgressBar) findViewById(R.id.pB1);

    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_browser);
    browser=(WebView)findViewById(R.id.layout_browser);

    browser.getSettings().setJavaScriptEnabled(true);
    browser.getSettings().setPluginsEnabled(true);
    browser.getSettings().setPluginState(WebSettings.PluginState.ON);

    browser.setWebChromeClient(new WebChromeClient() {
        public void onProgressChanged(WebView view, int progress) 
           {
           if(progress < 100 && Pbar.getVisibility() == ProgressBar.GONE){
               Pbar.setVisibility(ProgressBar.VISIBLE);
           }
           Pbar.setProgress(progress);
           if(progress == 100) {
               Pbar.setVisibility(ProgressBar.GONE);
           }
        }
    });

    browser.loadUrl("google.com");

并在我的 webview 布局中添加一个进度条,但它崩溃了,为什么?

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout
 xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<ProgressBar android:id="@+id/pB1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" 
    android:layout_centerVertical="true"
    android:padding="2dip">
</ProgressBar>
<WebView android:id="@+id/layout_browser" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
/>   
</RelativeLayout>

注意:我将隐藏我的状态栏,这就是我尝试使用此方法的原因

4

1 回答 1

2

这一行Pbar = (ProgressBar) findViewById(R.id.pB1);应该在之后setContentView(R.layout.layout_browser);

就像是,

setContentView(R.layout.layout_browser);
Pbar = (ProgressBar) findViewById(R.id.pB1);
于 2012-11-19T12:31:08.557 回答