基于这个例子Simple Cursor Adapter problem 我认为错误与 xml 布局有关
数据库详细信息:
public static final String question = "quest";
private static final String DATABASE_NAME = "QuestionDB";
private static final String DATABASE_TABLE = "questions";
DBAdapter da = new DBAdapter(this);
// open the database connection
da.open();
// assign to the cursor the ability to loop through the records.
Cursor c = da.getAllContacts();
方法 getAllContacts(); 基于以下代码
public Cursor getAllContacts()
{
return db.query(DATABASE_TABLE, new String[] {KEY_ROWID,
question, possibleAnsOne,possibleAnsTwo, possibleAnsThree,realQuestion}, null, null, null, null, null);
}
startManagingCursor(c);
// move to the first element
c.moveToFirst();
String[] from = new String[]{"quest"};
int[] to = new int[] { R.id.name_entry };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.layout_list_example, c, from, to);
setListAdapter(sca);
这是 xml 文件 layout_list_example.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@android:id/android:list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
list_example_entry.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/name_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="@+id/number_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
</LinearLayout>
扩展内容设置内容审查消除了该问题。我会记住这一点,以备将来之用。我不得不将列改回 quest,因为它说不存在列,当我运行活动时,我得到一个黑屏,上面有线条,我猜是一个没有数据的列表视图。
这是错误日志
05-12 19:52:22.570: E/AndroidRuntime(534): FATAL EXCEPTION: main
05-12 19:52:22.570: E/AndroidRuntime(534): java.lang.RuntimeException: Unable to start activity ComponentInfo{alex.android.basic.databse.questions/alex.android.basic.databse.questions.Result}: java.lang.IllegalArgumentException: column 'question' does not exist
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.os.Looper.loop(Looper.java:137)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.ActivityThread.main(ActivityThread.java:4424)
05-12 19:52:22.570: E/AndroidRuntime(534): at java.lang.reflect.Method.invokeNative(Native Method)
05-12 19:52:22.570: E/AndroidRuntime(534): at java.lang.reflect.Method.invoke(Method.java:511)
05-12 19:52:22.570: E/AndroidRuntime(534): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-12 19:52:22.570: E/AndroidRuntime(534): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-12 19:52:22.570: E/AndroidRuntime(534): at dalvik.system.NativeStart.main(Native Method)
05-12 19:52:22.570: E/AndroidRuntime(534): Caused by: java.lang.IllegalArgumentException: column 'question' does not exist
05-12 19:52:22.570: E/AndroidRuntime(534): at android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:267)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:332)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.widget.SimpleCursorAdapter.<init>(SimpleCursorAdapter.java:81)
05-12 19:52:22.570: E/AndroidRuntime(534): at alex.android.basic.databse.questions.Result.onCreate(Result.java:37)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.Activity.performCreate(Activity.java:4465)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-12 19:52:22.570: E/AndroidRuntime(534): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)