这是我的 ExpandableList 活动的代码
public class ExpenseEditorActivity extends ExpandableListActivity {
MyDOA projedit = new MyDOA(ExpenseEditorActivity.this, Environment.getExternalStorageDirectory());
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
lv_Expenses = getExpandableListView();
projedit.open();
fillExpenseList();
lv_Expenses.setAdapter(exp_adapter);
}
public void fillExpenseList(){
Cursor groupCursor=projedit.fetchGroup(SelectProjectActivity.sel_id);
startManagingCursor(groupCursor);
groupCursor.moveToFirst();
exp_adapter=new MyExpandableListAdapter(groupCursor,this, R.layout.expense_list_expandableitem, R.layout.expense_list_item,new String[]{"expdate"}, new int[]{R.id.label_date},new String[]{"expname","expamount"},new int[]{R.id.label_eh,R.id.label_amount});
}
public class MyExpandableListAdapter extends SimpleCursorTreeAdapter {
public MyExpandableListAdapter(Cursor cursor, Context context,int groupLayout,
int childLayout, String[] groupFrom, int[] groupTo, String[] childrenFrom, int[] childrenTo) {
super(context, cursor, groupLayout, groupFrom, groupTo,childLayout, childrenFrom, childrenTo);
}
@Override
protected Cursor getChildrenCursor(Cursor groupCursor) {
Cursor childCursor= projedit.fetchChildren(SelectProjectActivity.sel_id, groupCursor.getString(0));
startManagingCursor(childCursor);
childCursor.moveToFirst();
return childCursor;
}
}
}
只要可展开列表视图未展开(这是默认设置),活动运行良好但当我单击“组”展开时,我收到以下 logcat 错误
FATAL EXCEPTION: main java.lang.IllegalStateException: get field slot from row 1 col -1 failed
at android.database.CursorWindow.getLong_native(Native Method)
at android.database.CursorWindow.getLong(CursorWindow.java:412)
at android.database.AbstractWindowedCursor.getLong(AbstractWindowedCursor.java:108)
at android.widget.CursorTreeAdapter$MyCursorHelper.getId(CursorTreeAdapter.java:437)
at android.widget.CursorTreeAdapter.getGroupId(CursorTreeAdapter.java:192)
at android.widget.ExpandableListConnector.getItemId(ExpandableListConnector.java:421)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:1992)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:130)
at android.app.ActivityThread.main(ActivityThread.java:3687)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:507)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
at dalvik.system.NativeStart.main(Native Method)
有人可以阅读此内容并告诉我为什么单击任何“组”时会出现这些错误。
此外,如果我在我的代码中将所有组设置为扩展。活动一开始,我就会得到同样的错误(显然)
编辑
public Cursor fetchChildren(int i,String date){
Cursor cursor=database.query("expense,expensehead", new String[]{"expense._id","expensehead.expname","expense.expamount","expensehead._id"},
"expense.project_id=\""+i+"\" and expensehead.project_id=\""+i+"\" and expense.exphead_id=expensehead._id and expense.expdate=\""+date+"\"", null, null, null, null);
return cursor;
}
public Cursor fetchGroup(int i){
Cursor cursor=database.query("expense", new String[]{"expdate"}, "project_id=\""+i+"\"",null, "expense.expdate", null, null);
return cursor;
}
我的数据库看起来像这样
database.execSQL("create table project(_id integer primary key autoincrement, "+
"name text not null, " +
"amount real not null, " +
"startdate text not null, " +
"enddate text not null, "+
"avgbase integer not null);");
database.execSQL("create table expensehead(_id integer primary key autoincrement, "+
"expname text not null, "+
"project_id integer not null);");
database.execSQL("create table expense(_id integer primary key autoincrement, "+
"expamount real not null, "+
"expdate text not null, "+
"exphead_id integer not null, "+
"project_id integer not null);");