简体   繁体   中英

Set a progress bar in full screen webview Android

i tried to use this solutions Android WebView progress bar

which gives me

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

and adding a progress bar in my webview layout but it crashes, why ?

<?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>

Note : i will hide my status bar that's why I tried to use this method

This line Pbar = (ProgressBar) findViewById(R.id.pB1); should be after setContentView(R.layout.layout_browser);

Something like,

setContentView(R.layout.layout_browser);
Pbar = (ProgressBar) findViewById(R.id.pB1);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM