个ListView使用MatrixCursor和SimpleCu

个ListView使用MatrixCursor和SimpleCu

本文介绍了在文本和图像一个ListView使用MatrixCursor和SimpleCursorAdapter的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用一个问题一个 MatrixCursor 来填充我的的ListView

I'm having an issue using a MatrixCursor to populate my 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

Constructing the SimpleCursorAdapter causes a crash. Even when I tried removing the icon the app still crashed. Here is my 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>

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

Here is the call stack at the time of the crash:

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]

解决方法:

我发现这个问题,解决的办法是在我的回答如下。

I found the problem and the solution is in my answer below.

推荐答案

让我们粉笔这一个缺乏经验与使用Eclipse调试Java。

Let's chalk this one up to inexperience with debugging Java using Eclipse.

在运行调试器的应用程序,我开车撞一个RuntimeException。点击调用堆栈最顶端的元素给了我变量的列表,在其中我看到我的例外五。

Running the application in the debugger, I crashed with a RuntimeException. Clicking the very top element in the call stack gave me the list of Variables, at which I saw my Exception e.

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

The specific error was an InvalidArgument because my MatrixCursor did not have an _id column. Adding a column labeled _id fixed the problem and now everything works.

谢谢你让我看一下调试器了!要坦然面对,并熟知你的工具!

Thanks for making me look at the debugger again! Be comfortable with and knowledgeable about your tools!

这篇关于在文本和图像一个ListView使用MatrixCursor和SimpleCursorAdapter的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 01:23