【问题标题】:Using MatrixCursor and SimpleCursorAdapter in a ListView with text and images在带有文本和图像的 ListView 中使用 MatrixCursor 和 SimpleCursorAdapter
【发布时间】:2010-12-25 07:50:03
【问题描述】:

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

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

<?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>

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

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]  

解决方案:

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

【问题讨论】:

  • 当您遇到崩溃时,您可以查看堆栈跟踪以帮助找出问题所在——使用 adb logcat、DDMS 或 Eclipse 中的 DDMS 透视图。如果这不能为您提供足够的帮助来解决问题,请将堆栈跟踪作为对您问题的编辑发布,因为它可能会给我们一些线索。
  • 很好的问题......我希望我在两周前找到这个!感谢您的示例代码。干得好!

标签: java android matrixcursor


【解决方案1】:

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

在调试器中运行应用程序时,我因 RuntimeException 而崩溃。单击调用堆栈中最顶部的元素给了我变量列表,在该列表中我看到了我的异常 e。

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

感谢您让我再次查看调试器!熟悉并熟悉您的工具!

【讨论】:

    猜你喜欢
    • 2012-08-03
    • 2011-12-28
    • 1970-01-01
    • 2011-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-20
    相关资源
    最近更新 更多