0

基于这个例子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)
4

2 回答 2

0

这是不正确的 String[] from = new String[]{"quest"};

String[] 必须是光标中某一列的名称,因为它用于将数据从光标映射到列表视图。

我想你想要这个:

String[] from = new String[]{"question"};

编辑

如果找不到,那么我猜您需要为活动设置内容视图。

setContentView(R.layout.layout_list_example);

编辑 2

我认为我们一直在盯着它看却没有看到它...在您的查询中,将每个项目括在引号中,除了 KEY_ 一个。“问题”等

于 2012-05-12T12:58:03.233 回答
0

如果你使用ListActivty你的布局 xml 应该有带有 id 的 listviewandroid:id="@android:id/list"

例如:

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>

所以改变android:id="@android:id/android:list"layout_list_example.xmlandroid:id="@android:id/list"应该可以解决你的问题

希望这可以帮助

于 2012-05-12T15:48:00.193 回答