2012-04-15 124 views
2

我正在使用ListActivity,listview。Android - ListActivity,添加頁眉和頁腳視圖

listView = getListView(); 

只是完美的工作。我加了腳註視圖

LayoutInflater inflater = getLayoutInflater(); 
listView.addFooterView(inflater.inflate(R.layout.footer, null), null, false); 

,一切都是閃亮的,但很醜,所以我想添加這個頁腳視圖(只包含1 EditText上只有1按鈕)爲標題的ListView

LayoutInflater inflater = getLayoutInflater(); 
listView.addHeaderView(inflater.inflate(R.layout.footer, null), null, false); 

並突然發生一切錯誤,並立即得到RuntimeException。

Suspended(exception RuntimeException) 
ActivityThread.performLaunchActivity(ActivityThread$ActivityRecord, Intent) 
ActivityThread.handleLaunchActivity(ActivityThread$ActivityRecord, Intent) 
ActivityThread.access$2200(ActivityThread, Activity$ActiviyRecord, Intent), 
so on.. 

爲什麼它會拋出異常? addFooterView和addHeaderView有什麼不同,以及如何將標題添加到ListActivity?

UPDATE

所以你可以在評論閱讀,我的logcat仍然不能正常工作,但我只是想未來在這一刻:

} catch(Exception e){ 
    Writer result = new StringWriter(); 
    PrintWriter printWriter = new PrintWriter(result); 
    e.printStackTrace(printWriter); 
    String error = result.toString(); 
} 

後來我把斷點,我可以讀取表達式部分中的錯誤。它說:

java.lang.IllegalStateException: Cannot add header view to list -- setAdapter has already been called. 

這對我們所有人都是有益的。在改變命令之後,它可以正常工作。

+0

@add f在這裏,你的logcat ... – 2012-04-15 15:07:46

+0

logcat根本不工作:S從來沒有對我工作 – Victor 2012-04-15 15:14:02

+0

@Victor:「logcat根本不工作:S從來沒有工作過我」 - 你的意思是你不知道如何訪問logcat數據?啓動任何仿真器或將手機連接到PC並在eclipse中使用DDMS,您將看到每個正在運行的應用程序都會將數據輸出到logcat。 – Squonk 2012-04-15 15:24:13

回答

-6

所以如果你想添加標題視圖到你的listView,你應該在使用setListAdapter()之前做到這一點,否則它會導致IllegalStateException。

+9

在anticafe提供相同的答案2周後,您回答了您自己的問題,只是將其選爲正確的答案。這根本不符合SO的精神。 – 2014-01-26 18:31:01

+1

是的,如果你閱讀評論和更新,你可以看到我自己找到了答案。我想回答這個問題,但由於我的低信譽,我無法回答。畢竟,你記下了我的正確答案。這根本不在SO的頭腦中。 – Victor 2014-03-23 16:29:51

+0

沒有勝利者,你沒有提出任何評論,所以首先說。即使你這樣做了,你也應該感謝有人爲你提供了明確的答案。 – 2015-07-30 20:33:37

11

當你登錄

java.lang.IllegalStateException:無法頭視圖添加到列表 - setAdapter已經被調用。

ListView控件的方法addHeaderViewaddFooterView前必須setAdapter被調用。

+0

是的,我也想補充答案,但是我必須等待8個小時才能回答我自己的問題,因爲我的信譽低。 – Victor 2012-04-15 17:34:08

0

創建佈局XML頁眉和頁腳

header_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
    <TextView 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:gravity="center_horizontal" 
    android:text="header" 
    /> 
    </RelativeLayout> 

footer_layout.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<TextView 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center_horizontal" 
android:text="footer" 
/> 
</RelativeLayout> 

現在在活動Java文件onCreate()方法添加

listView = (ListView) findViewById(R.id.listView1); 
LayoutInflater inflater = getLayoutInflater(); 
    ViewGroup header = (ViewGroup) inflater.inflate(R.layout.header, listView, 
      false); 
    ViewGroup footer = (ViewGroup) inflater.inflate(R.layout.footer, listView, 
      false); 
    listView.addHeaderView(header, null, false); 
    listView.addFooterView(footer, null, false); 
    listView.setAdapter(adapter);