android - Palette class is crashing my app on some images -


i making google i/o 2014 music player, , having trouble color extraction album art. here albums screen class:

package com.animbus.music;  import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.porterduff; import android.graphics.drawable.bitmapdrawable; import android.graphics.drawable.drawable; import android.os.bundle; import android.support.v7.app.appcompatactivity; import android.support.v7.graphics.palette; import android.view.menu; import android.view.menuitem; import android.widget.imagebutton; import android.widget.imageview;  import java.security.spec.pssparameterspec;   public class albums_activity extends appcompatactivity {      public bundle b;     public string albumname;     public string albumartist;     public int albumart;     public int primarycolor;     public int accentcolor;     public int titletextcolor;     public int subtitletextcolor;     public int fabicon;     public palette palette;      protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_albums_activity);         //get intent data , set easier handle , use         b = getintent().getextras();         albumname = b.getstring("album_name");         albumartist = b.getstring("album_artist");         albumart = b.getint("album_art");         drawable albumartdrawable = getdrawable(albumart);         bitmap albumart = bitmapfactory.decoderesource(getresources(), albumart);          imageview albumartview = (imageview) findviewbyid(r.id.albums_activity_albumart);         imagebutton playall = (imagebutton) findviewbyid(r.id.play_all_fab);         //make sure colors aren't null         primarycolor = getresources().getcolor(r.color.accent);         accentcolor = getresources().getcolor(r.color.accent);         titletextcolor = getresources().getcolor(r.color.accent);         subtitletextcolor = getresources().getcolor(r.color.accent);         fabicon = getresources().getcolor(r.color.accent);          //convert palette resources         palette = palette.from(albumart).generate();          primarycolor = palette.getvibrantswatch().getrgb();         accentcolor = palette.getlightvibrantswatch().getrgb();         titletextcolor = palette.getvibrantswatch().gettitletextcolor();         subtitletextcolor = palette.getvibrantswatch().getbodytextcolor();         fabicon = palette.getlightvibrantswatch().gettitletextcolor();          //toolbar, setting toolbar actionbar,setting arrow shown, , setting title nothing         android.support.v7.widget.toolbar toolbar = (android.support.v7.widget.toolbar) findviewbyid(r.id.album_toolbar);         android.support.v7.widget.toolbar infotoolbar = (android.support.v7.widget.toolbar) findviewbyid(r.id.album_info_toolbar);         setsupportactionbar(toolbar);         //noinspection constantconditions         getsupportactionbar().setdisplayhomeasupenabled(true);         toolbar.settitle("");         //sets title intent's data         infotoolbar.settitle(albumname);         infotoolbar.setsubtitle(albumartist);         //sets albumart         albumartview.setimageresource(albumart);         //sets color of info toolbar based on albumart         infotoolbar.setbackgroundcolor(primarycolor);         infotoolbar.settitletextcolor(titletextcolor);         infotoolbar.setsubtitletextcolor(subtitletextcolor);         //sets accent color based on album art         playall.getbackground().setcolorfilter(accentcolor, porterduff.mode.src_atop);         playall.getdrawable().setcolorfilter(fabicon,porterduff.mode.src_atop);     }      @override     public boolean oncreateoptionsmenu(menu menu) {         // inflate menu; adds items action bar if present.         getmenuinflater().inflate(r.menu.menu_albums_activity, menu);         return true;     }      @override     public boolean onoptionsitemselected(menuitem item) {         // handle action bar item clicks here. action bar         // automatically handle clicks on home/up button, long         // specify parent activity in androidmanifest.xml.         int id = item.getitemid();          //noinspection simplifiableifstatement         if (id == r.id.action_settings) {             return true;         }          return super.onoptionsitemselected(item);     } } 

here problem. have app 3 buttons, album, album alt, , album alt 2. album alt opens. others throw:

java.lang.runtimeexception: unable start activity componentinfo{com.animbus.music/com.animbus.music.albums_activity}: java.lang.nullpointerexception: attempt invoke virtual method 'int android.support.v7.graphics.palette$swatch.getrgb()' on null object reference 

and app crashes. says problem at:

primarycolor = palette.getvibrantswatch().getrgb(); 

i think might because of palette.getvibrantswatch().getrgb() equaling null. don't know please help, have disable palette , continue making app please help

its not necessary palette return vibrantswatch. check null before calling getrgb().

palette.swatch vibrantswatch = palette.getvibrantswatch();   if(vibrantswatch != null) {     //get rgb   } else {     //get swatch } 

you can swatches available palette using getswatches(). then, used color, pick swatch having maximum pixel population. can pixel population calling getpopulation() on swatches.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -