Cor*_*y D 21 java android matrixcursor
我有一个问题,使用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);
}
Run Code Online (Sandbox Code Playgroud)
构造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>
Run Code Online (Sandbox Code Playgroud)
编辑:这是崩溃时的调用堆栈:
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]
Run Code Online (Sandbox Code Playgroud)
解:
我发现了问题,解决方案在我的答案中.
Cor*_*y D 20
让我们来讨论使用Eclipse调试Java的经验.
在调试器中运行应用程序,我遇到了RuntimeException崩溃.单击调用堆栈中的顶部元素给了我变量列表,我在其中看到了我的异常e.
特定错误是InvalidArgument,因为我的MatrixCursor没有_id列.添加标记为_id的列可以解决问题,现在一切正常.
感谢您让我再次查看调试器!熟悉并熟悉您的工具!