简体   繁体   中英

custom action bar image - app crashes

Hi Im having trouble with setting custom background on my action bar. I wan for every single intent custom image on acton bar.

I've already tried this

final ActionBar actionBar = getActionBar();
    BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.my_picture));
    actionBar.setBackgroundDrawable(background);

but the app crashes with error

          java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.lowrider.menuapp/com.example.lowrider.menuapp.MapsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference

You should use getSupportActionBar() and not getActionBar().

BitmapDrawable background = new BitmapDrawable(BitmapFactory.decodeResource(getResources(), R.drawable.my_picture));
getSupportActionBar().setBackgroundDrawable(background);

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