Question

I am trying to add a progressbar to a webview class.However, even after following all steps correctly, it crashes with a null pointer exception, any idea why? Here's my mainactivity:

public class MainActivity extends Activity {
    WebView webview;
    ProgressBar progress; 
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
       // getWindow().requestFeature(Window.FEATURE_PROGRESS);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.main);

         webview = (WebView)findViewById(R.id.webview);
        webview.setInitialScale(1);
        WebSettings webSettings = webview.getSettings();
        webSettings.setJavaScriptEnabled(true);
        progress = (ProgressBar) webview.findViewById(R.id.ProgressBar);
        progress.setVisibility(View.VISIBLE);
        webview.getSettings().setJavaScriptEnabled(true);
        webview.getSettings().setLoadWithOverviewMode(true);
        webview.getSettings().setUseWideViewPort(true);
      //  webview.setWebViewClient(new MyCustomWebViewClient());

        webview.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
        webview.setScrollbarFadingEnabled(false);
       webview.getSettings().setRenderPriority(RenderPriority.HIGH);
        webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
       // webview.getSettings().setBlockNetworkLoads(true);
        webview.getSettings().setBuiltInZoomControls(true); 
        webview.loadUrl("http://www.yahoo.com");

        webview.getSettings().setSupportZoom(false);

       /* webview.setWebChromeClient(new WebChromeClient()
        {
         public void onProgressChanged(WebView view, int progress)
         {
           // update the progressBar
           MainActivity.this.setProgress(progress * 100);
         }
        });*/
    }
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            switch(keyCode)
            {
            case KeyEvent.KEYCODE_BACK:
                if(webview.canGoBack() == true){
                    webview.goBack();
                }else{
                    finish();
                }
                return true;
            }

        }
        return super.onKeyDown(keyCode, event);
    }

    private class MyCustomWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            progress.setVisibility(View.VISIBLE);
          }
          public void onPageFinished(WebView view, String url) {
            progress.setVisibility(View.GONE);
          }
    }

}

and here's the corresponding xml: main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >
<WebView  
    android:id="@+id/webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>
 <ProgressBar
    android:id="@+id/ProgressBar"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:maxHeight="30dp"
    android:minHeight="30dp" />
</RelativeLayout>
Was it helpful?

Solution

Change

progress = (ProgressBar) webview.findViewById(R.id.ProgressBar);

to

progress = (ProgressBar)findViewById(R.id.ProgressBar);

Progressbar belongs to main.xml. findViewById looks for a view in the current inflated layout. You have set the content of layout main.xml to your activity.

OTHER TIPS

I see that you copied an example, but you did it wrong. Replace the commented code with this:

webView.setWebChromeClient(new WebChromeClient()
{
    public void onProgressChanged(WebView view, int prog)
    {
        progress.setProgress(prog * 100);
    }
});

Also, as said in the other post, that webview in webview.findViewById needs to be removed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top