Tôi đang gặp một vấn đề bằng cách sử dụng MatrixCursor
để cư trú của tôi ListView
:Sử dụng MatrixCursor và SimpleCursorAdapter trong một ListView với văn bản và hình ảnh
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);
}
Xây dựng các SimpleCursorAdapter
gây ra một vụ tai nạn. Ngay cả khi tôi đã cố gắng xóa biểu tượng, ứng dụng vẫn gặp sự cố. Đây là tôi 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: Dưới đây là các cuộc gọi stack tại thời điểm vụ tai nạn:
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:
Tôi thấy vấn đề và giải pháp là trong câu trả lời của tôi dưới đây.
Khi bạn gặp sự cố, bạn có thể xem dấu vết ngăn xếp để tìm ra nơi mọi thứ đang diễn ra sai - sử dụng adb logcat, DDMS hoặc phối cảnh DDMS trong Eclipse. Nếu điều đó không cung cấp cho bạn đủ trợ giúp để khắc phục sự cố, hãy đăng theo dõi ngăn xếp dưới dạng bản chỉnh sửa cho câu hỏi của bạn, vì nó có thể cung cấp cho chúng tôi một số manh mối. – CommonsWare
Câu hỏi hay ... Tôi ước tôi đã tìm thấy điều này hai tuần trước! Cảm ơn mã mẫu. Làm tốt lắm!! – mobibob