2011-10-10 74 views
2

我能夠顯示列表視圖從一個array.But當我點擊任何行應用程序崩潰說ClassCaste異常android.widget.linearlayout。 我的XML:無法在列表視圖中使用findViewById從列表視圖訪問textView onItemClick

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" android:layout_height="wrap_content"> 
<ImageView android:id="@+id/icon" android:layout_height="wrap_content" 
    android:src="@drawable/icon" android:layout_width="22px" 
    android:layout_marginTop="4px" android:layout_marginRight="4px" 
    android:layout_marginLeft="4px"> 
</ImageView> 
<TextView android:text="@+id/TextView01" android:layout_width="wrap_content" 
    android:layout_height="wrap_content" android:id="@+id/label" 
    android:textSize="30px"></TextView> 


特定的代碼中可能存在的錯誤:

ArrayAdapter<String> countryAdapter=new ArrayAdapter<String>(this,R.layout.rowlayout,R.id.label,countries); 
    setListAdapter(countryAdapter); 
    ListView lv = getListView(); 
    lv.setTextFilterEnabled(true); 
    lv.setOnItemClickListener(this); 

和的onclick方法,其中我試圖顯示從列表視圖中clickd項目敬酒:

@Override 
public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    // TODO Auto-generated method stub 
    Toast.makeText(getApplicationContext(), ((TextView)linearlayout.findViewById(R.id.label)).getText(), Toast.LENGTH_SHORT).show(); 

} 

當我點擊任何列表項目時,應用程序崩潰。

有什麼建議嗎? 感謝

+0

什麼ü在這個部分的? Toast.makeText(getApplicationContext(),((TextView)linearlayout.findViewById(R.id.label))。getText(),Toast.LENGTH_SHORT).show(); 'onItemClick'方法中的 – Venky

+0

,你從哪裏得到'linearLayout'引用? – anirvan

+0

試圖訪問線性佈局的子節點(已經聲明),然後將其轉換爲textview.my dumb mind :( – Alok

回答

2

更換

((TextView)linearlayout.findViewById(R.id.label)).getText() 

通過

((TextView)view.findViewById(R.id.label)).getText() 

希望工程:)