2013-05-03 40 views
0

即時通訊新的Android開發和需要幫助填充我的ListView。 數據庫方法返回一個列表數組,我需要將「名稱」和「性別」放入自定義列表視圖 ListView中。 這些代碼的相關章節:嘗試從MySQLite填充ListView。獲取錯誤

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 
+0

你介意分享這些錯誤嗎? – Johannes 2013-05-03 15:53:22

+0

您的Suspectview是否擴展了ListActivity?刪除擴展可能會有所幫助。 – 2013-05-03 17:10:45

+0

是的,它是一個擴展。它不再是一個擴展我刪除了所有代碼,並重新實現了所有相同的錯誤發生 – user2340360 2013-05-03 17:15:15

回答

0

例外情況:「您的內容必須具有ID屬性爲'android.R.id.list'的ListView」。嘗試將ListView的id更改爲該值。

+0

我編輯到上面,仍然得到相同的錯誤 – user2340360 2013-05-03 16:46:04