2012-04-23 56 views
0

在Android中的2.12.3它工作正常。但對於3.0及以上findViewById沒有找到第二個ListView。我已經嘗試清理該項目,嘗試不使用ListActivity,沒有任何幫助。有任何想法嗎?Android 3.0及以上版本中的兩個ListViews在一個活動中

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    setContentView(R.layout.main); 
    setListAdapter(List1Adaptor); 
    list1 = (ListView) findViewById(android.R.id.list); 

    list2 = (ListView) findViewById(R.id.ListView2); 
    list2.setAdapter(List2Adapter);//Null Pointer 
} 

main.xml中

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/linearLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
<ListView 
    android:id="@+id/ListView2" 
    android:layout_height="wrap_content" 
    android:layout_width="fill_parent" > 
</ListView> 


</LinearLayout> 
<RelativeLayout 
android:id="@+id/listLayout_relativeLayout2" 
android:layout_height="320dp" 
android:layout_width="fill_parent" > 

<ListView    
    android:id="@android:id/list" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
</ListView> 
</RelativeLayout> 
+0

您是否使用適配器? – njzk2 2012-04-24 16:45:48

+0

是的,我做到了...... – Adri 2012-04-25 17:49:55

回答

0

您的xml文件缺少根視圖。我很驚訝它適用於任何版本的Android。你應該用線性佈局或類似的東西包圍一切。

+0

我沒有發佈。它太大了。但這實際上是我根本沒有嘗試的,重新排列main.xml。我有很多線性和相對佈局交叉。謝謝 – Adri 2012-04-25 17:57:38

+0

我解決了它。好吧,我猜一些佈局是問題。只需要刪除無用的。 感謝您的幫助! – Adri 2012-04-26 00:28:01

0

你的佈局有android:id="@+id/listView2"(注意:小寫字母 'L')和Java代碼請求R.id.ListView2(注:大寫字母 'L')。在兩種情況下嘗試使用相同的情況。

+0

對此抱歉,在嘗試簡化代碼時出現錯誤。我確實改變了它。 – Adri 2012-04-23 23:19:50

+0

沒人能幫忙嗎? – Adri 2012-04-24 08:08:29

相關問題