Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

appcompat show progress in action bar causes NPE

Tags:

android

After updating my SDK to all the latest Android 5.0 goodies I can't use progress bars built into the ActionBar in appcompat. I have done all the usual fixed (move supportRequestWindowFeature() call to before setContent() and before super call in oncreate) but nothing works. Here is what I'm doing:

public class LoginActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
    supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
    super.onCreate(savedInstanceState);

    setContentView(R.layout.login);
    ...
    loginButton.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
                ...
                setSupportProgressBarIndeterminateVisibility(true);
        }
    });

} and the stack trace:

10-18 19:38:21.053: E/AndroidRuntime(11206): java.lang.NullPointerException: Attempt to invoke     virtual method 'void android.support.v7.internal.widget.ProgressBarCompat.setVisibility(int)' on a null object reference
10-18 19:38:21.053: E/AndroidRuntime(11206):    at android.support.v7.app.ActionBarActivityDelegateBase.updateProgressBars(ActionBarActivityDelegateBase.java:786)
10-18 19:38:21.053: E/AndroidRuntime(11206):    at android.support.v7.app.ActionBarActivityDelegateBase.setSupportProgressBarIndeterminateVisibility(ActionBarActivityDelegateBase.java:692)
10-18 19:38:21.053: E/AndroidRuntime(11206):    at android.support.v7.app.ActionBarActivity.setSupportProgressBarIndeterminateVisibility(ActionBarActivity.java:327)
10-18 19:38:21.053: E/AndroidRuntime(11206):    at com.myapppackage.LoginActivity$2.onClick(LoginActivity.java:82)

This is on a Nexus 5 running Android 4.4.4. The app theme inherits from Theme.AppCompat. The app is built with Android 5.0 and targetSDK is 21. When I use setSupportProgress for a normal horizontal progress bar the same thing happens. Any help much appreciated.

EDIT: Found the problem. In android.support.v7.internal.widget.ToolbarWidgetWrapper:

@Override
public void initIndeterminateProgress() {
    Log.i(TAG, "Progress display unsupported");
}

Maybe not a bug but a feature? Toolbars seem to be the new ActionBars.

I have a copy of V20 appcompat on another computer so I'm going back to that.

like image 622
Mark Avatar asked Oct 18 '14 19:10

Mark


2 Answers

A possible workaround for this would be to manually add a ProgressBar as a custom view and override setSupportProgressBarIndeterminateVisibility

In onCreate:

ProgressBar progressBar = new ProgressBar(this);
progressBar.setVisibility(View.GONE);
progressBar.setIndeterminate(true);
getSupportActionBar().setDisplayShowCustomEnabled(true);
getSupportActionBar().setCustomView(progressBar);

In your activity:

@Override
public void setSupportProgressBarIndeterminateVisibility(boolean visible) {
    getSupportActionBar().getCustomView().setVisibility(visible ? View.VISIBLE : View.GONE);
}

Of course switching to a Toolbar and adding the ProgressBar there should be more future-proof.

like image 129
ov3rk1ll Avatar answered Oct 25 '22 10:10

ov3rk1ll


It looks like indeterminate progress and horizontal progress bar are not supported in support library V21. From the android.support.v7.internal.widget.ToolbarWidgetWrapper:

@Override
public void initIndeterminateProgress() {
    Log.i(TAG, "Progress display unsupported");
}

Chris Banes has confirmed this: http://chris.banes.me/2014/10/17/appcompat-v21/#comment-1642002459. I am using an old copy of appcompat v20. Here is a zip for anyone who needs it: https://drive.google.com/file/d/0B2dSjU9N8KdZUEFuMkhLZzJZOFU/view?usp=sharing

like image 45
Mark Avatar answered Oct 25 '22 12:10

Mark