【问题标题】:Click listView Item Error @ ID #0x5?单击列表查看项目错误@ ID #0x5?
【发布时间】:2011-05-03 00:23:37
【问题描述】:

我收到错误消息“找不到资源异常:字符串资源 ID #0x5。我知道它与 i.putExtra(...) 有关。我正在尝试从数据库列“gotoURL”中提取一个字符串,以将一个 URL 字符串从我的 ListView 中的 listItem 传递到 WebView Activity。任何帮助PLZ!谢谢!

我是我的活动:

    lv.setTextFilterEnabled(true);
    lv.setOnItemClickListener(new OnItemClickListener() {
        // @Override
        public void onItemClick(AdapterView<?> a, View v, int position,long id) 
        {
            Object o = lv.getItemAtPosition(position);
            Toast.makeText(List_AC.this, "Clicked!", Toast.LENGTH_LONG).show();
            Intent i = new Intent(List_AC.this, DocView.class);
            Cursor cursor = Adapter_AC.dataCursor;
            i.putExtra("url", getString(cursor.getColumnIndexOrThrow("gotoURL")));
            startActivity(i);
        }
    });

适配器:

public class Adapter_AC extends SimpleCursorAdapter {


static Cursor dataCursor;
private LayoutInflater mInflater;

public Adapter_AC(Context context, int layout, Cursor dataCursor,
        String[] from, int[] to) {
    super(context, layout, dataCursor, from, to);
    this.dataCursor = dataCursor;
    mInflater = LayoutInflater.from(context);
}

public View getView(int position, View convertView, ViewGroup parent) {

    ViewHolder holder;

    if (convertView == null) {
        convertView = mInflater.inflate(R.layout.list_item, null);

        holder = new ViewHolder();
        holder.text1 = (TextView) convertView.findViewById(R.id.label);
        holder.text2 = (TextView) convertView.findViewById(R.id.listTitle);
        holder.text3 = (TextView) convertView.findViewById(R.id.caption);
        holder.text4 = (TextView) convertView.findViewById(R.id.dummy);

        holder.text4.setVisibility(View.GONE);

        convertView.setTag(holder);
    } else {
        holder = (ViewHolder) convertView.getTag();
    }

    dataCursor.moveToPosition(position);

    int label_index = dataCursor.getColumnIndex("label");
    String label = dataCursor.getString(label_index);

    int title_index = dataCursor.getColumnIndex("title");
    String title = dataCursor.getString(title_index);

    int description_index = dataCursor.getColumnIndex("description");
    String description = dataCursor.getString(description_index);

    int goto_index = dataCursor.getColumnIndex("gotoURL");
    String gotoURL = dataCursor.getString(goto_index);

    holder.text1.setText(label);
    holder.text2.setText(title);
    holder.text3.setText(description);
    holder.text4.setText(gotoURL);

    return convertView;
}

static class ViewHolder {
    TextView text1;
    TextView text2;
    TextView text3;
    TextView text4;
    protected static final String KEY_TITLE = "text4";
}

}

【问题讨论】:

  • 究竟是哪一行引发了异常?

标签: android listview webview adapter onitemclicklistener


【解决方案1】:

问题可能出在 onItemClickListner 中:

i.putExtra("url", getString(cursor.getColumnIndexOrThrow("gotoURL")));

您要求列索引(一个整数),将其用作 Context.getString() 的输入,该 Context.getString() 期望这是一个(现有的)资源 ID。从你的错误我可以猜到“gotoURL”的 columnIndex 是 5 并且不存在这样的资源 id...

我猜您真正想在意图中添加的是“gotoURL”列的字符串值,就像您在适配器中所做的那样?

【讨论】:

  • & @dmon Thnx,这是真的,我最终一起改变了一切。请查看here。再次感谢。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-06-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多