首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在带有文本和图像的MatrixCursor和SimpleCursorAdapter中使用ListView

在带有文本和图像的MatrixCursor和SimpleCursorAdapter中使用ListView
EN

Stack Overflow用户
提问于 2009-12-10 16:19:10
回答 1查看 14.5K关注 0票数 21

我在使用MatrixCursor填充ListView时遇到了问题

代码语言:javascript
复制
private void fillData() {
    String[] menuCols = new String[] { "icon", "item", "price" };
    int[] to = new int[] { R.id.icon, R.id.item, R.id.price };

    MatrixCursor menuCursor = new MatrixCursor(menuCols);
    startManagingCursor(menuCursor);

    menuCursor.addRow(new Object[] { R.drawable.chicken_sandwich, "Chicken Sandwich", "$3.99" });

    SimpleCursorAdapter menuItems = new SimpleCursorAdapter(
            this, R.layout.menu_row, menuCursor, menuCols, to);

    setListAdapter(menuItems);
}

构造SimpleCursorAdapter会导致崩溃。即使当我尝试删除图标,应用程序仍然崩溃。这是我的menu_row.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    xmlns:android="http://schemas.android.com/apk/res/android">
    <ImageView 
        android:id="@+id/icon"
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </ImageView>
    <TextView 
        android:id="@+id/item" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
    <TextView 
        android:id="@+id/price" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content">
    </TextView>
</LinearLayout>

编辑:这里是崩溃时的调用堆栈:

代码语言:javascript
复制
Thread [<3> main] (Suspended (exception RuntimeException))  
    ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2481  
    ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) line: 2497   
    ActivityThread.access$2200(ActivityThread, ActivityThread$ActivityRecord, Intent) line: 119 
    ActivityThread$H.handleMessage(Message) line: 1848  
    ActivityThread$H(Handler).dispatchMessage(Message) line: 99 
    Looper.loop() line: 123 
    ActivityThread.main(String[]) line: 4338    
    Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]  
    Method.invoke(Object, Object...) line: 521  
    ZygoteInit$MethodAndArgsCaller.run() line: 860  
    ZygoteInit.main(String[]) line: 618 
    NativeStart.main(String[]) line: not available [native method]  

解决方案:

我发现了问题,解决办法就在下面的答案中。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-12-10 18:15:59

让我们将此归因于缺乏使用Eclipse调试Java的经验。

在调试器中运行应用程序时,我使用了一个RuntimeException。单击调用堆栈中最上面的元素将给出变量列表,在其中我看到了异常e。

具体的错误是InvalidArgument,因为我的MatrixCursor没有_id列。添加一个标记为_id的列解决了这个问题,现在一切都正常了。

谢谢你让我再次看到调试器!熟悉和了解你的工具!

票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1882156

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档