2014-12-02 85 views
0

我想在列表視圖中顯示數據庫的多個列。我正在使用sqliteasset助手和一個row.xml和main.xml。我的主要活動擴展了列表活動。我得到的錯誤是,findviewbyid無法找到一個線性佈局與「R.id.list」,但我的activity_main.xml有它。以列表視圖顯示預填充數據庫

這是我的主要類,它顯示列表上的數據庫信息。

public class MainActivity extends ListActivity { 

    private Cursor schedule; 
    private MyDatabase db; 
    ListView listView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     db = new MyDatabase(this); 
     schedule = db.getSchedule(); 

     ListView listView = (ListView) findViewById(android.R.id.list); 
     listView.setAdapter(new SimpleCursorAdapter(
      this, // The context 
      R.layout.row, // The layout of the row to show 
      db.getSchedule(), // Here the cursor where the data is 
           // but it can be anything 
      new String[] {"fName", "fType" }, // here the tables to show 
      new int[] { android.R.id.text1, android.R.id.text2 }, // here where to show? the IDs 
              // of the layout elements where put data. 
      0)); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     schedule.close(); 
     db.close(); 
    } 
} 

行:

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     android:text="TextView" 
     android:textColor="#0000FF" 
     android:textSize="25dp" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginBottom="10dp" 
     android:layout_marginTop="10dp" 
     android:text="TextView" 
     android:textColor="#0000FF" 
     android:textSize="25dp" /> 

</LinearLayout> 

Activity_Main:(你可以看到id.list)

<?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="fill_parent" 
> 
<ListView 
android:id="@+id/list" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
</ListView> 
</LinearLayout> 

的logcat:

12-02 01:04:44.436: E/AndroidRuntime(1183): FATAL EXCEPTION: main 
12-02 01:04:44.436: E/AndroidRuntime(1183): Process: com.example.mealplan, PID: 1183 
12-02 01:04:44.436: E/AndroidRuntime(1183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mealplan/com.example.mealplan.MainActivity}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.os.Looper.loop(Looper.java:136) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at java.lang.reflect.Method.invoke(Method.java:515) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at dalvik.system.NativeStart.main(Native Method) 
12-02 01:04:44.436: E/AndroidRuntime(1183): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ListActivity.onContentChanged(ListActivity.java:243) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:293) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.Activity.setContentView(Activity.java:1929) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at com.example.mealplan.MainActivity.onCreate(MainActivity.java:35) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.Activity.performCreate(Activity.java:5231) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
12-02 01:04:44.436: E/AndroidRuntime(1183):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
+0

閱讀本http://developer.android.com/reference/android/app/ListActivity.html/。 – Raghunandan 2014-12-02 06:22:51

回答

0

改變這一行:

android:id="@+id/list" 

android:id="@id/android:list" 
+0

我的程序不再崩潰,它顯示兩個「文本視圖」旁邊eachother!下一步是讓他們說出正確的東西。爲什麼我需要手動更改?就像它有什麼不同,所以我不會再犯這個錯誤。 – Snoballin 2014-12-02 06:24:11

+0

ListActivity需要一個在android R類中定義的預定義id。您使用@ + id定義了一個額外的ID,ListActivity不會將其識別爲正確的ID。 – 2014-12-02 06:29:46

0

您正在嘗試獲取來自Android的默認庫列表ID。您應該從您自己的佈局中檢索列表ID。

改變這一行

ListView listView = (ListView) findViewById(android.R.id.list); 

ListView listView = (ListView) findViewById(R.id.list); 
相關問題