【问题标题】:(exception Resources$NotFoundException))ListView.layoutChildren() line: 1596(异常 Resources$NotFoundException))ListView.layoutChildren() 行:1596
【发布时间】:2011-06-06 01:02:51
【问题描述】:

我正在尝试以完全编程的方式(不使用任何 xml)创建带有自定义列表项单元格的列表。我遇到了以下错误。在网上搜索了几个小时后,我将这篇文章作为我在 Stack Overflow 上的第一个问题。

感谢您对此的意见以及关于如何在不使用 R.*.xml 的情况下更好地处理 Android 编程的任何建议

在运行此代码时,我收到以下错误:

(暂停(异常 Resources$NotFoundException))

ListView.layoutChildren() 行:1596

ListView(AbsListView).onLayout(boolean, int, int, int, int) 行:1112

ListView(View).layout(int, int, int, int) 行:6569

FrameLayout.onLayout(boolean, int, int, int, int) 行:333 ...

代码:

公共类 MyList 扩展 ListActivity{

static final ArrayList<HashMap<String,String>> list = 
    new ArrayList<HashMap<String,String>>();

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

//自定义列表

    LinearLayout mainLayout=new LinearLayout(this);
    ListView myListView=new ListView(this);
    mainLayout.addView(myListView);


    LinearLayout ListItemLayout=new LinearLayout(this);
    TextView title = new TextView(this);
    TextView author = new TextView(this);
    TextView price = new TextView(this);
    ListItemLayout.addView(title);
    ListItemLayout.addView(author);
    ListItemLayout.addView(price);

    SimpleAdapter adapter = new SimpleAdapter(
        this,
        list,
        ListItemLayout.getId(),
        new String[] {"title","author","price"},
        new int[] {ListItemLayout.getChildAt(0).getId(),ListItemLayout.getChildAt(1).getId(),ListItemLayout.getChildAt(2).getId()}
    );     
    populateList();
    setListAdapter(adapter);  

}

private void populateList() {
    HashMap<String,String> map = new HashMap<String,String>();
    map.put("title",
"Professional Android ");
    map.put("author", "Meier");
    map.put("price", "$54.99");
    list.add(map);
    map = new HashMap();
    map.put("title","Android 2");
    map.put("author", 
"Sayed");
    map.put("price", "$48.98");
    list.add(map);
    }

}

【问题讨论】:

    标签: android android-layout


    【解决方案1】:

    SimpleAdapter 的构造函数需要 ListItemLayout 中的 id,但它没有(我猜 Android 回退是检查 R 文件),所以在代码中为其分配一个 id。

    【讨论】:

    • 为 ListItemLayout 和 TextViews 添加了 setId。仍然得到同样的错误.. LinearLayout ListItemLayout=new LinearLayout(this); ListItemLayout.setId(1002); TextView 标题 = new TextView(this);标题.setId(1003); TextView 作者 = new TextView(this);作者.setId(1004); TextView 价格 = new TextView(this);价格.setId(1005); ListItemLayout.addView(title); ListItemLayout.addView(作者); ListItemLayout.addView(price);
    【解决方案2】:

    你不能那样做。 SimpleAdapter 需要 xml 中定义的布局中的资源 ID。它还需要将要填充的 TextViews 的资源 ID。这是因为它使用LayoutInflater#inflate 创建包含View,然后使用ViewGroup#findViewById 获取对将显示数据的TextViews 的引用。

    如果您想在代码中完成所有布局,包括 Adapter 的代码,您必须创建自己的自定义 Adapter 类。您可以通过扩展BaseAdapter 并至少覆盖getView 来做到这一点。

    【讨论】:

    • 在创建自定义适配器后完美运行。谢谢 Qberticus!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-03
    • 2011-04-14
    • 1970-01-01
    • 1970-01-01
    • 2020-09-17
    相关资源
    最近更新 更多