尝试从SQLite填充ListView。得到错误

时间:2013-05-03 15:42:22

标签: java android mysql

我是Android开发的新手,需要帮助填充我的ListView。 数据库方法返回一个List数组,我需要将“name”和“sex”放入自定义中 列表显示。 这些是代码的相关部分:

DatabaseHandler.Java

   public List<Suspect> getAllContacts() {
        List<Suspect> suspectList = new ArrayList<Suspect>();
        // Select All Query
        String selectQuery = "SELECT  * FROM " + TABLE_SUSPECTS;

        SQLiteDatabase db = this.getReadableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {
                Suspect suspect = new Suspect();
                suspect.setID(Integer.parseInt(cursor.getString(0)));
                suspect.setName(cursor.getString(1));
                suspect.setSex(cursor.getString(2));
                suspect.setHeight(cursor.getString(3));
                suspect.setAge(cursor.getString(4));
                suspect.setHair(cursor.getString(5));
                suspect.setAdditional(cursor.getString(6));
                suspect.setNationality(cursor.getString(7));
                suspect.setType(cursor.getString(8));
                // Adding suspect to list
                suspectList.add(suspect);
            } while (cursor.moveToNext());
        }
        // return suspect list
        return suspectList;

        }

Suspectview.java

                     DatabaseHandler db = new DatabaseHandler(this);    
         List<Suspect> lol = db.getAllContacts();
         listView = (ListView) findViewById(R.id.suspectView);


             List<String> stock_list = new ArrayList<String>();
             stock_list.add("stock1");
             stock_list.add("stock2");
             ArrayList<String> your_array_list = new ArrayList<String>();
             your_array_list = stock_list.toArray((your_array_list));
             for(String s : your_array_list)
                 System.out.println(s)
             ArrayAdapter<String> arrayAdapter =      
             new ArrayAdapter<String>(this,R.layout.suspect_list,your_array_list);
             listView.setAdapter(arrayAdapter); 

Suspect_list.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <!-- Suspect id / Hidden by default -->
    <TextView
    android:id="@+id/suspect_id"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:visibility="gone" />

    <!-- Suspect Name -->

     <!-- Suspect Image -->

    <ImageView
       android:contentDescription="@string/name"
       android:id="@+id/suspect_image"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_alignParentLeft="true"
       android:layout_alignParentTop="true"
       android:adjustViewBounds="true"
       android:background="#9ed321"
       android:maxHeight="80dp"
       android:maxWidth="80dp"
       android:paddingLeft="3dip"
       android:paddingRight="3dip"
       android:src="@drawable/iconcontactadd"
       android:textColor="#ffffff"
       android:textStyle="bold" />


     <TextView
        android:id="@+id/suspect_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/suspect_image"
        android:layout_marginLeft="21dp"
        android:layout_toRightOf="@+id/suspect_image"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />


      <TextView
         android:id="@+id/suspect_sex"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_alignBaseline="@+id/suspect_name"
         android:layout_alignBottom="@+id/suspect_name"
         android:layout_marginLeft="20dp"
         android:layout_toRightOf="@+id/suspect_name"
         android:text="Small Text"
         android:textAppearance="?android:attr/textAppearanceSmall" />

      </RelativeLayout>

activity_suspect_view.xml

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent" >

       <include layout="@layout/header" 
          android:id="@+id/header"
          />

     <ListView
         android:id="@+id/suspectView"
         android:layout_below="@+id/header" 
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:divider="#b5b5b5"
         android:dividerHeight="1dp"
         android:cacheColorHint="#00000000"/>


        </RelativeLayout>

logcat的。

05-03 15:18:18.023: D/dalvikvm(369): GC_EXTERNAL_ALLOC freed 75K, 52% free 2599K/5379K, external 1917K/2137K, paused 40ms
05-03 15:18:18.074: D/AndroidRuntime(369): Shutting down VM
05-03 15:18:18.074: W/dalvikvm(369): threadid=1: thread exiting with uncaught exception (group=0x40015560)
05-03 15:18:18.093: E/AndroidRuntime(369): FATAL EXCEPTION: main
05-03 15:18:18.093: E/AndroidRuntime(369): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sherlock/com.example.sherlock.Suspectview}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.os.Looper.loop(Looper.java:123)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-03 15:18:18.093: E/AndroidRuntime(369):  at java.lang.reflect.Method.invokeNative(Native Method)
05-03 15:18:18.093: E/AndroidRuntime(369):  at java.lang.reflect.Method.invoke(Method.java:507)
05-03 15:18:18.093: E/AndroidRuntime(369):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-03 15:18:18.093: E/AndroidRuntime(369):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-03 15:18:18.093: E/AndroidRuntime(369):  at dalvik.system.NativeStart.main(Native Method)
05-03 15:18:18.093: E/AndroidRuntime(369): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ListActivity.onContentChanged(ListActivity.java:243)
05-03 15:18:18.093: E/AndroidRuntime(369):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:210)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.Activity.setContentView(Activity.java:1657)
05-03 15:18:18.093: E/AndroidRuntime(369):  at com.example.sherlock.Suspectview.onCreate(Suspectview.java:28)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-03 15:18:18.093: E/AndroidRuntime(369):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-03 15:18:18.093: E/AndroidRuntime(369):  ... 11 more
05-03 15:18:19.903: I/Process(369): Sending signal. PID: 369 SIG: 9

2 个答案:

答案 0 :(得分:0)

例外:&#34;您的内容必须有一个ListView,其id属性为&#39; android.R.id.list&#39;&#34;。尝试将ListView的ID更改为此值。

答案 1 :(得分:0)

您以错误的方式使用ListActivity:请参阅http://developer.android.com/guide/topics/ui/layout/listview.html#Example

您应该只是扩展Activity而不是ListActivity。

在您的问题评论中,您说您已将其更改为“活动”。但是,您的LogCat输出仍然在ListActivity.onContentChanged(...)中显示错误。所以?