Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

setSupportProgressBarIndeterminateVisibility raising java.lang.NullPointerException when used with Android 5.0 SDK (API 21)

Tags:

My activity class extends android.support.v7.app.ActionBarActivity. I am requesting window feature and calling setSupportProgressBarIndeterminateVisibility() in the onCreate() method as follows:

protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);     setContentView(R.layout.activity_main);     setSupportProgressBarIndeterminateVisibility(true); } 

I cannot grasp what's wrong with my code and why I am getting java.lang.NullPointerException raised by the setSupportProgressBarIndeterminateVisibility().

My gradle dependencies contains:

compile 'com.android.support:appcompat-v7:21.0.0' 

Does anybody know how to use the indeterminate progress bar in the support.v7 action bar with the API 21?

like image 506
Lubos Ilcik Avatar asked Oct 19 '14 18:10

Lubos Ilcik


2 Answers

You need to use Toolbar instead of ActionBar and add the ProgressBar into the Toolbar.

Here is an easy solution to insert indeterminate ProgressBar into Toolbar; turns out it's not difficult at all :) Just put your ProgressBar xml element inside your Toolbar like this:

<android.support.v7.widget.Toolbar     xmlns:android="http://schemas.android.com/apk/res/android"     android:id="@+id/toolbar"     android:layout_height="wrap_content"     android:layout_width="match_parent"     android:minHeight="?attr/actionBarSize"     android:background="?attr/colorPrimary">      <ProgressBar         android:id="@+id/progress_spinner"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="end"         android:indeterminate="true"         android:visibility="gone" />  </android.support.v7.widget.Toolbar> 

And then in your AppCompatActivity, simply retrieve the ProgressBar after retrieving the Toolbar and set the ProgressBar to visible or invisible when you need it.

protected void onCreate(Bundle savedInstanceState)  {     setContentView(R.layout.toolbar);      mToolbar = (Toolbar) findViewById(R.id.toolbar);     setSupportActionBar(mToolbar);      progressBar = (ProgressBar) findViewById(R.id.progress_spinner);      //Make progress bar appear when you need it     progressBar.setVisibility(View.VISIBLE);      //Make progress bar disappear     progressBar.setVisibility(View.INVISIBLE); } 

Hope this helps :)

EDIT: replaced ActionBarActivity with AppCompatActivity as per the latest Android support libraries guidelines.

like image 69
ABVincita Avatar answered Dec 31 '22 03:12

ABVincita


Per this comment on Chris Banes' (the author of AppCompat) AppCompat v21 announcement post:

Either way, progress bar's are not supported on Toolbar anymore.

On why:

Because Toolbar is a highly focused widget. If you want a ProgressBar, add it to the Toolbar yourself (it's just a ViewGroup).

I'd expect more information as the official documentation gets updated here shortly.

like image 32
ianhanniballake Avatar answered Dec 31 '22 04:12

ianhanniballake