开发者社区> 问答> 正文

附加image时 Text View 的 Resource Id 出错

在spinner中我想附加一个image 和一个 text View
下面是 layout xml 代码:
screenshot
Custom Array Adapter 使用的构造函数:
screenshot
当 activity 开启时,spinner 没有显示 image 和text,但是当我选择 spinner 时,给出错误 Adapter need resouce id for the text view
如何解决这个问题?

展开
收起
蛮大人123 2016-02-18 10:51:07 2467 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪
    /**
     * Constructor
     *
     * @param context The current context.
     * @param textViewResourceId The resource ID for a layout file containing a TextView to use when
     * instantiating views.
     * @param objects The objects to represent in the ListView.
     */
     public ArrayAdapter(Context context, int textViewResourceId, T[] objects) {
     init(context, textViewResourceId, 0, Arrays.asList(objects));
     }
    这是ArrayAdapter的构造方法。
    
    你第二个参数直接传递了一个Layout,而构造方法需要的是一个TextView的Id
    这下面是内部代码 
    ...
    mFieldId = textViewResourceId;
    ...
    try {
     if (mFieldId == 0) {
     // If no custom field is assigned, assume the whole resource is a TextView
     text = (TextView) view;
     } else {
     // Otherwise, find the TextView field within the layout
     text = (TextView) view.findViewById(mFieldId);
     }
     } catch (ClassCastException e) {
     Log.e("ArrayAdapter", "You must supply a resource ID for a TextView");
     throw new IllegalStateException(
     "ArrayAdapter requires the resource ID to be a TextView", e);
     }
    2019-07-17 18:43:47
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
AutoAlerts-From Data to Action 立即下载
OPEN SOURCE IN A DATA-DRIVEN WORLD 立即下载
低代码开发师(初级)实战教程 立即下载