java.lang.RuntimeException:无法启动Activity {package_name.ActivityClass}:java.IllegalArgumentException:列'_id'不存在

时间:2013-02-26 09:43:42

标签: android android-layout simplecursoradapter

我在Sqlite数据库中有两个表。第一个表的列为_idname。第二个表格的列为_cidCategory

我正在使用ListView将第二个表格中的数据检索到SimpleCursorAdapter。在日志中,我看到了RuntimeExceptionjava.IllegalArgumentException: column '_id' does not exist

的活动:

public class ActivityClass extends ListActivity{

private CursorAdapter curCateAdapter;
private ListView cateListview;
SimpleCursorAdapter adapter;    
@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    cateListview=getListView();
    final DatabaseConnector database = new DatabaseConnector(NoteCategoryActivity.this);
    Cursor myCursor=database.getAllCategory();
    String[] from = new String[] {"Category"};
    int[] to = new int[] { R.id.cateTextView };
    adapter= new SimpleCursorAdapter(this, R.layout.category_list, 
                    myCursor, 
                    from, to);
    cateListview.setAdapter(adapter);
}
}

DatabaseHelper上课:

public class DatabaseHelper {

private static final String DB_NAME = "NameDB.db";
private static final String TABLE_NAME1 = "table1";
private static final String TABLE_NAME2 = "Categories";
private static final String KEY_ID="_id";
private static final String KEY_NAME="Name";
public static final String KEY_NCID="_cid";
public static final String KEY_CAT="Category";
private SQLiteDatabase database;
private DatabaseOpenHelper dbOpenHelper;

    public Cursor getAllCategory() {
    database=dbOpenHelper.getWritableDatabase();
    Cursor mCursor =database.query("Categories", new String[]{"_cid","Category"},  null, null, null, null, null);
    if (mCursor != null) {
mCursor.moveToFirst();
          }
    return mCursor;
}

category_list.xml中,我定义了此文件中使用的RelativeLayout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
     > 
    <TextView xmlns:android="http://schemas.android.com/apk/res/android"
       android:id="@+id/cateTextView" 
       android:layout_width="fill_parent"
       android:layout_height="wrap_content" 
       android:textSize="18sp" 
       android:gravity="center_vertical"
      />
</RelativeLayout>

logcat输出:

02-26 15:17:35.729: E/AndroidRuntime(17757): FATAL EXCEPTION: main
02-26 15:17:35.729: E/AndroidRuntime(17757): java.lang.RuntimeException: Unable to start activity ComponentInfo{packagename.MyActivity}: java.lang.IllegalArgumentException: column '_id' does not exist
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.os.Looper.loop(Looper.java:123)
02-26 15:17:35.729: E/AndroidRuntime(17757):at android.app.ActivityThread.main(ActivityThread.java:3683)
02-26 15:17:35.729: E/AndroidRuntime(17757):at java.lang.reflect.Method.invokeNative(Native Method)
02-26 15:17:35.729: E/AndroidRuntime(17757):at java.lang.reflect.Method.invoke(Method.java:507)
02-26 15:17:35.729: E/AndroidRuntime(17757):at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at dalvik.system.NativeStart.main(Native Method)
02-26 15:17:35.729: E/AndroidRuntime(17757): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
02-26 15:17:35.729: E/AndroidRuntime(17757):    at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:314)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at android.widget.CursorAdapter.init(CursorAdapter.java:111)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at android.widget.CursorAdapter.<init>(CursorAdapter.java:90)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:47)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:84)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at intuition.itlabs.app.makenotes.MyActivity.onCreate(NoteCategoryActivity.java:39)
02-26 15:17:35.729: E/AndroidRuntime(17757):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

1 个答案:

答案 0 :(得分:1)

SimpleCursorAdapter需要_id列才能正常工作。在选择数据时,您应该为您的id列添加别名:“_ cid as _id”。 Use google.