解決Your content must have a ListView whose id attribute is 'android.R.id.list'

來源:互聯網
上載者:User

標籤:

1.錯誤提示:Your content must have a ListView whose id attribute is ‘android.R.id.list‘

 

對於以上錯誤,其實可能是因為我們要實現對ListView中setOnItemClick的事件監聽而去繼承了LiseActivity,但是卻沒有ListView的標籤,只要在布局檔案中添加定製Layout的代碼,即將ListView的id設定為“@android:id/list”或android:id="@id/android:list",這時可以用getListView來擷取ListView的對象了。

xml代碼如下:


[html] view plaincopy <ListView  
android:id="@android:id/list" 或android:id="@id/android:list" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content"> 
</ListView> 


2.同樣容易報此錯誤的還有TabHost,如下:

 

問題1.  運行Activity的時候出現Your content must have a TabHost whose id attribute is ‘android.R.id.tabhost’

添加Layout的時候,xml跟元素選擇TabHost, 但是ADT沒有添加id屬性, 啟動並執行時候,會提示Your content must have a TabHost

whose id attribute is ‘android.R.id.tabhost’錯誤, 需要添加android:id=”@android:id/tabhost”, 這樣就可以了。

 

問題2. 運行Activity的時候出現Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabcontent’

解決方案: 修改FrameLayout添加id屬性, ADT自動產生的xml檔案中Id是android:id=”@+id/FrameLayout01 ”, 需要修改成下

面的格式android:id=”@android:id/tabcontent ”,這個估計會困擾一大批初學者,誰會想到會修改這個地方,看到錯誤很容易

修改成tabcontent,但是首碼不容易想到。 而且在ADT可視化編輯這個檔案的時候, 介面上顯示NullPointerException,這個是

ADT的一個BUG。

 

 

修改後的xml如下:

 

<?xml version=”1.0″ encoding=”utf-8″?>
<TabHost
android:id=”@android:id/tabhost”
xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”fill_parent” android:layout_height=”fill_parent”>
<LinearLayout android:id=”@+id/LinearLayout01″ android:layout_height=”fill_parent”

android:layout_width=”fill_parent” android:orientation=”vertical”>

<TabWidget android:id=”@android:id/tabs” android:layout_height=”wrap_content”

android:layout_width=”fill_parent”>
</TabWidget>
<FrameLayout android:id=”@android:id/tabcontent” android:layout_width=”fill_parent”

android:layout_height=”fill_parent”>
</FrameLayout>
</LinearLayout>
</TabHost>

 

 

注意: 如果用TabHost的話, 上面標紅的三處必須是一樣, 這個是Google的約定。  而且一個工程中只能有一個TabHost。

來源:http://blog.sina.com.cn/s/blog_5e58565701010bpj.html

解決Your content must have a ListView whose id attribute is 'android.R.id.list'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.