4

I stumbled upon this tutorial about in-app notifications on the ActionBar.

http://www.jmhend.me/layerdrawable-menuitems

All I get is NullPointer inside android.graphics.drawable.LayerDrawable when calling item.getIcon() on the line mentioned in this log: http://pastebin.com/udKPtUzB

I think it could have something to do with Android API version, but I cannot find a solution as I cannot figure out whats returning "null".

I should mention this does not happen on startup of the app. It only happen when I try to update the drawable to show a count.

I've come to find out this is due to item.getIcon() which should return the Drawable of the menu item. But it's always null during runtime. (not startup though)

public boolean onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu) { 
    com.actionbarsherlock.view.MenuInflater inflater = getSupportMenuInflater();
    inflater.inflate(R.menu.main, menu);
    menu.findItem(R.id.action_list).setIcon(
            new IconDrawable(this, IconValue.fa_list).colorRes(
                    android.R.color.black).actionBarSize()
    );
    com.actionbarsherlock.view.MenuItem item = menu.findItem(R.id.action_users);
    LayerDrawable icon;
    if (item.getIcon() != null) {
        icon = (LayerDrawable) item.getIcon();
        // Update LayerDrawable's BadgeDrawable
        setBadgeCount(MainActivity.this, icon, privateMsgNotificationCount);
    } else {
    }
    return true;
}

NPE at if(item.getIcon()!=null) now. The condition never gets tested because getIcon() causes NPE as in the pastebin log mentioned above.

NOTE: After 12 hrs of testing all I can say for certain is that whenever I setIcon() of the MenuItem as a LayerDrawable, getIcon() is null. If I setIcon() to ANY OTHER type of drawable the system will remember the MenuItem's drawable and getIcon is NOT null. I'm puzzled...

TonyCruze
  • 441
  • 4
  • 12

0 Answers0