我正在尝试将SQLite数据加载到listview中。但是当调用数据库retirevalval活动时,我的应用程序停止。
这是我的Logcat
03-02 11:05:04.488: I/dalvikvm(24942): threadid=3: reacting to signal 3
03-02 11:05:04.579: I/dalvikvm(24942): Wrote stack traces to '/data/anr/traces.txt'
03-02 11:05:04.898: I/dalvikvm(24942): threadid=3: reacting to signal 3
03-02 11:05:04.918: I/dalvikvm(24942): Wrote stack traces to '/data/anr/traces.txt'
03-02 11:05:05.118: D/gralloc_goldfish(24942): Emulator without GPU emulation detected.
03-02 11:05:06.978: D/AndroidRuntime(24942): Shutting down VM
03-02 11:05:06.978: W/dalvikvm(24942): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-02 11:05:07.048: E/AndroidRuntime(24942): FATAL EXCEPTION: main
03-02 11:05:07.048: E/AndroidRuntime(24942): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.grammer/com.example.grammer.Database}: java.lang.IllegalArgumentException: column '_id' does not exist
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.ActivityThread.access$600(ActivityThread.java:123)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.os.Handler.dispatchMessage(Handler.java:99)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.os.Looper.loop(Looper.java:137)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.ActivityThread.main(ActivityThread.java:4424)
03-02 11:05:07.048: E/AndroidRuntime(24942): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 11:05:07.048: E/AndroidRuntime(24942): at java.lang.reflect.Method.invoke(Method.java:511)
03-02 11:05:07.048: E/AndroidRuntime(24942): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-02 11:05:07.048: E/AndroidRuntime(24942): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-02 11:05:07.048: E/AndroidRuntime(24942): at dalvik.system.NativeStart.main(Native Method)
03-02 11:05:07.048: E/AndroidRuntime(24942): Caused by: java.lang.IllegalArgumentException: column '_id' does not exist
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.widget.CursorAdapter.init(CursorAdapter.java:168)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.widget.CursorAdapter.<init>(CursorAdapter.java:145)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.widget.ResourceCursorAdapter.<init>(ResourceCursorAdapter.java:91)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:104)
03-02 11:05:07.048: E/AndroidRuntime(24942): at com.example.grammer.Database.displayListView(Database.java:81)
03-02 11:05:07.048: E/AndroidRuntime(24942): at com.example.grammer.Database.onCreate(Database.java:38)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.Activity.performCreate(Activity.java:4465)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
03-02 11:05:07.048: E/AndroidRuntime(24942): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
03-02 11:05:07.048: E/AndroidRuntime(24942): ... 11 more
03-02 11:05:07.178: I/dalvikvm(24942): threadid=3: reacting to signal 3
03-02 11:05:07.199: I/dalvikvm(24942): Wrote stack traces to '/data/anr/traces.txt'
03-02 11:05:07.678: I/dalvikvm(24942): threadid=3: reacting to signal 3
03-02 11:05:07.698: I/dalvikvm(24942): Wrote stack traces to '/data/anr/traces.txt'
03-02 11:05:08.099: I/dalvikvm(24942): threadid=3: reacting to signal 3
03-02 11:05:08.109: I/dalvikvm(24942): Wrote stack traces to '/data/anr/traces.txt'
03-02 11:05:09.149: I/Process(24942): Sending signal. PID: 24942 SIG: 9
调用的活动是数据库活动。 具体如下。
public class Database extends Activity {
private DatabaseHandler dbHelper;
private SimpleCursorAdapter dataAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.exercise);
dbHelper = new DatabaseHandler(this);
dbHelper.open();
displayListView();
}
private void displayListView() {
Cursor cursor = dbHelper.fetchQuestions();
// The desired columns to be bound
String[] columns = new String[] {
DatabaseHandler.KEY_ROWID,
DatabaseHandler.KEY_NAME,
DatabaseHandler.KEY_TITLE,
DatabaseHandler.KEY_ANSWER1,
DatabaseHandler.KEY_ANSWER2,
DatabaseHandler.KEY_ANSWER3,
DatabaseHandler.KEY_ANSWER4,
DatabaseHandler.KEY_ANSWER,
DatabaseHandler.KEY_ANSWER_TEXT
};
// the XML defined views which the data will be bound to
int[] to = new int[] {
R.id.id,
R.id.title,
R.id.question,
R.id.ans1,
R.id.ans2,
R.id.ans3,
R.id.ans4,
R.id.ans,
R.id.ansnum,
};
// create the adapter using the cursor pointing to the desired data
//as well as the layout information
dataAdapter = new SimpleCursorAdapter(
this, R.layout.questions_info,
cursor,
columns,
to,
0);
ListView listView = (ListView) findViewById(R.id.listView1);
// Assign adapter to ListView
listView.setAdapter(dataAdapter);
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> listView, View view,
int position, long id) {
// Get the cursor, positioned to the corresponding row in the result set
Cursor cursor = (Cursor) listView.getItemAtPosition(position);
// Get the state's capital from this row in the database.
String question =
cursor.getString(cursor.getColumnIndexOrThrow("code"));
Toast.makeText(getApplicationContext(),
question, Toast.LENGTH_SHORT).show();
}
});
}
}
我的数据库处理程序类
public class DatabaseHandler{
// Contacts Table Columns names
static final String KEY_ROWID = "id";
static final String KEY_NAME = "questions";
static final String KEY_TITLE = "question_title";
public static final String KEY_ANSWER1 = "answer1";
public static final String KEY_ANSWER2 = "answer2";
public static final String KEY_ANSWER3 = "answer3";
public static final String KEY_ANSWER4 = "answer4";
public static final String KEY_ANSWER = "answer";
public static final String KEY_ANSWER_TEXT = "answer_text";
private static final String TAG = "DatabaseHandler";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
// All Static variables
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "grammer";
// Contacts table name
private static final String TABLE_QUESTIONS = "questions";
private final Context mCtx;
private static final String DATABASE_CREATE =
"CREATE TABLE if not exists " + TABLE_QUESTIONS + "("
+ KEY_ROWID +" integer PRIMARY KEY autoincrement," + KEY_NAME + " TEXT,"
+ KEY_ANSWER1 + " TEXT," + KEY_ANSWER2 + " TEXT," + KEY_ANSWER3 + " TEXT,"
+ KEY_ANSWER4 + " TEXT," + KEY_ANSWER + " TEXT," + KEY_TITLE + " TEXT," + KEY_ANSWER_TEXT + " TEXT" +")";;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + TABLE_QUESTIONS);
onCreate(db);
}
}
public DatabaseHandler(Context ctx) {
this.mCtx = ctx;
}
public DatabaseHandler open() throws SQLException {
mDbHelper = new DatabaseHelper(mCtx);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
if (mDbHelper != null) {
mDbHelper.close();
}
}
public long addQuestion(String Question, String Answer1, String Answer2, String Answer3, String Answer4, String Answer, String Answer_Text, String Question_Title) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_NAME, Question); // question Name
initialValues.put(KEY_ANSWER1, Answer1); // answer
initialValues.put(KEY_ANSWER2, Answer2); // answer
initialValues.put(KEY_ANSWER3, Answer3); // answer
initialValues.put(KEY_ANSWER4, Answer4); // answer
initialValues.put(KEY_ANSWER, Answer); // answer
initialValues.put(KEY_ANSWER_TEXT, Answer_Text);
initialValues.put(KEY_TITLE, Question_Title);
return mDb.insert(TABLE_QUESTIONS, null, initialValues);
}
public boolean deleteAllQuestions() {
int doneDelete = 0;
doneDelete = mDb.delete(TABLE_QUESTIONS, null , null);
Log.w(TAG, Integer.toString(doneDelete));
return doneDelete > 0;
}
public Cursor fetchQuestions() {
Cursor mCursor = mDb.query(TABLE_QUESTIONS, new String[] {KEY_ROWID,
KEY_NAME, KEY_ANSWER1, KEY_ANSWER2, KEY_ANSWER3, KEY_ANSWER4, KEY_ANSWER, KEY_ANSWER_TEXT, KEY_TITLE },
null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
我的.xml页面。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="ID: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:text="Question Title: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:text="Question: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView3"
android:text="Answer1: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView4"
android:text="Answer2: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView5"
android:layout_below="@+id/textView5"
android:text="Answer3: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView6"
android:layout_below="@+id/textView6"
android:text="Answer4: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView10"
android:layout_below="@+id/textView10"
android:text="Answer Number: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView7"
android:layout_below="@+id/textView7"
android:text="Correct Answer: "
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView1"
android:layout_alignBottom="@+id/textView1"
android:layout_toRightOf="@+id/textView1"
android:text="TextViewid" />
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView2"
android:layout_alignBottom="@+id/textView2"
android:layout_toRightOf="@+id/textView2"
android:text="TextViewtitle" />
<TextView
android:id="@+id/question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView3"
android:layout_toRightOf="@+id/textView3"
android:text="TextViewQuestion" />
<TextView
android:id="@+id/ans1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_toRightOf="@+id/textView4"
android:text="TextViewAns1" />
<TextView
android:id="@+id/ans2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/textView6"
android:layout_alignRight="@+id/ans1"
android:text="TextViewans2" />
<TextView
android:id="@+id/ans3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView6"
android:layout_alignBottom="@+id/textView6"
android:layout_alignLeft="@+id/question"
android:text="TextViewAns3" />
<TextView
android:id="@+id/ans4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView7"
android:layout_alignBottom="@+id/textView7"
android:layout_alignLeft="@+id/ans3"
android:text="TextViewAns4" />
<TextView
android:id="@+id/ans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/textView10"
android:layout_alignLeft="@+id/ansnum"
android:text="TextViewcorrectans" />
<TextView
android:id="@+id/ansnum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView9"
android:layout_alignBottom="@+id/textView9"
android:layout_alignParentRight="true"
android:text="TextViewcorrectansnumber" />
</RelativeLayout>
我可以弄清楚代码logcat说的是关于_id的一些我找不到的东西。
答案 0 :(得分:1)
引用文档
Cursor必须包含一个名为“_id”的列,否则该类不会 工作。此外,如果使用MergeCursor与此类不起作用 合并后的游标在“_id”列中具有重叠值。
http://developer.android.com/reference/android/widget/CursorAdapter.html
更改此
static final String KEY_ROWID = "id";
到
static final String KEY_ROWID = "_id";
答案 1 :(得分:0)
在这里您使用了SimpleCursorAdapter。要使用简单的游标适配器,您的查询中必须有一个名为_id
的列。否则SimpleCursorAdapter将无效。
因此,最好使用自己的CustomCursorAdapter。