Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
227 views
in Technique[技术] by (71.8m points)

java - Using MatrixCursor and SimpleCursorAdapter in a ListView with text and images

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);
}

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>

Edit: 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]  

SOLUTION:

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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.

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!


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...