<span style="font-size:12px;">PackageInfo packageInfo = packageInfos.get(position);
int icon = packageInfo.applicationInfo.icon;
if (icon!= 0) {
System.out.println(icon);
textView.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);
}else {
textView.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_launcher, 0, 0, 0);
}</span>
原本想在获取应用对应的图标id之后,通过
<span style="font-size:12px;">textView.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0);</span>
设置textView的图片,可是设置之后总是报错,找不到该资源。后来通过查看public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom)方法的源码。找到了原因:
<span style="font-size:12px;">public void setCompoundDrawablesWithIntrinsicBounds(int left, int top, int right, int bottom) {
<span style="color:#FF0000;"> final Resources resources = getContext().getResources();</span>
setCompoundDrawablesWithIntrinsicBounds(left != 0 ? resources.getDrawable(left) : null,
top != 0 ? resources.getDrawable(top) : null,
right != 0 ? resources.getDrawable(right) : null,
bottom != 0 ? resources.getDrawable(bottom) : null);
}</span>
从 final Resources resources = getContext().getResources();可知,获取的Resource是本应用的,而非系统的,再根据id肯定找不着应用对应的图标了