-1

I am trying to show a circular indeterminate loading indicator for a webView however despite reading a load of answers on here, my builds are failing. I get the error java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ProgressBar.setVisibility(int)' on a null object reference. I have set up the progress bar in the fragment.xml file like so:

<ProgressBar
    android:id="@+id/prgrsbar"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:visibility="gone"
    android:indeterminateDrawable="@drawable/progress" >
</ProgressBar>

and created an empty progress.xml file in drawable with this in it:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
</selector>

Finally, here is my fragment's onCreateView activity code that I am using:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    //code here



    View rootView = inflater.inflate(R.layout.fragment_first, container, false);

    String url = "http://winn-brown.co.uk/shmee/";
    WebView view = (WebView) rootView.findViewById(R.id.webview);
    view.getSettings().setJavaScriptEnabled(true);
    view.loadUrl(url);

    view.setWebViewClient(new WebViewClient() {

        private ProgressBar prgrsBar;
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            super.onPageStarted(view, url, favicon);
            prgrsBar.setVisibility(View.VISIBLE);
        }

        public void onPageFinished(WebView view, String url) {
            // do your stuff here
            Log.i("message", "website loaded");

        }
    });


    return rootView;
}

On this line private ProgressBar prgrsBar; it says "Private field 'prgrsBar' is never assigned, despite having the id in the fragment's xml file. Apologies if I am missing something obvious here.

Phantômaxx
  • 36,442
  • 21
  • 78
  • 108
dwinnbrown
  • 3,013
  • 7
  • 27
  • 53
  • prgrsBar variable is null .First you initialize it like this way. ProgressBar bar=(ProgressBar)findViewById(R.id.pgrsbar); – akhil Rao Jul 18 '16 at 09:25

2 Answers2

0

Just add

prgrsBar = (ProgressBar)rootView.findViewById(R.id.prgrsbar);

below

private ProgressBar prgrsBar; 
Akshay Panchal
  • 695
  • 5
  • 15
0

You need to get a reference of the progress bar from the xml file after this call in onCreateView

View rootView = inflater.inflate(R.layout.fragment_first, container, false);

Hải Nguyễn
  • 2,643
  • 1
  • 17
  • 34
  • I tried this `ProgressBar prgrsBar = (ProgressBar)rootView.findViewById(R.id.prgrsbar);` but it said variable 'prgrsBar' is never used. Maybe this is because it is outside the webview client? – dwinnbrown Jul 18 '16 at 09:33
  • where do you use that variable? – Hải Nguyễn Jul 18 '16 at 09:39
  • here `private ProgressBar prgrsBar; public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); prgrsBar.setVisibility(View.VISIBLE); }` – dwinnbrown Jul 18 '16 at 09:40
  • Move the `private ProgressBar prgrsBar` to the outer class (it should be a variable of the fragment) – Hải Nguyễn Jul 18 '16 at 09:40