Android - ResourcesNotFoundException when trying to set Action Bar icon by resource id

拥有回忆 提交于 2020-01-03 02:27:09

问题


I am changing my Action Bar icon to CircleImageView that shows profile picture:

CircleImageView actionBarIcon_Profile = new CircleImageView(this);
actionBarIcon_Profile.setImageDrawable(getResources().getDrawable(R.drawable.my_picture));
actionBarIcon_Profile.setLayoutParams(new LayoutParams(48, 48));
actionBarIcon_Profile.setId(R.id.actionBarIcon_Profile);

getActionBar().setIcon(R.id.actionBarIcon_Profile);

The ids are kept in ids.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <item type="id" name="actionBarIcon_Profile"/>
</resources>

I get ResourcesNotFoundException. Any help will be appreciated.


回答1:


setIcon is expecting to receive an id of a resource. That id actionBarIcon_Profile is just an unique id you're creating that has no resource associated with it. That's why you're getting ResourcesNotFoundException.

You can set a drawable to your actionbar's setIcon method. Not an imageview. You need to create a circular drawable instead of a circular imageView.



来源:https://stackoverflow.com/questions/26905618/android-resourcesnotfoundexception-when-trying-to-set-action-bar-icon-by-resou

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!