2013-07-04 76 views
3

我試圖打印已在ListView中單擊的值,但隨後出現以下異常:Android java.lang.ClassCastException:android.widget.LinearLayout不能轉換爲android.widget.TextView

07-04 10:40:56.482: E/AndroidRuntime(1356): FATAL EXCEPTION: main 
07-04 10:40:56.482: E/AndroidRuntime(1356): java.lang.ClassCastException:  android.widget.LinearLayout cannot be cast to android.widget.TextView 
07-04 10:40:56.482: E/AndroidRuntime(1356):  at com.passwordkeeper.ui.ActivityHomeScreen$1.onItemClick(ActivityHomeScreen.java:88) 

下面的代碼片段:

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    mTextView = (TextView) findViewById(R.id.labelList); 
    db = new DatabaseHandler(this); 
    createList(); 
    displayList(); 
} 

的createList功能是否工作正常。這裏是我的顯示列表的方法:

public void displayList(){ 
    this.setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_home_screen, R.id.listTextView, mAccountNames)); 
    mListView = getListView(); 
    mListView.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      String product = ((TextView) view).getText().toString(); 
      Log.v("mSelectedProduct", product); 
     }   
    }); 
} 

我對文件activity_home_screen.xml XML代碼:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" > 
<TextView 
    android:id="@+id/listTextView" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="28dp" 
    android:layout_marginTop="26dp" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

任何幫助將是有益的!

謝謝。

+0

在這裏發表您的XML代碼.. –

+0

mTextView應該是TextView的類型 –

+0

視圖返回文本字符串作爲產品,在onItemClick方法是給你classcast錯誤時拋出,檢查它。 – techieWings

回答

4

你可以試試這個

mListView.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      TextView txtview = (TextView)view.findViewById(R.id.listTextView); 
      String product = txtview.getText().toString(); 
      Log.v("mSelectedProduct", product); 
     }   
    }); 
+0

謝謝蘇尼爾:)工作。 –

1

使用此:

String product = ((TextView) view.findViewById(R.id.listTextView)).getText().toString(); 
0
mListView.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      TextView textview= (TextView)view.findViewById(R.id.TEXTVIEW_ID); 
      String product = textView.getText().toString(); 
      Log.v("mSelectedProduct", product); 
     }   
    }); 

替換 「TEXTVIEW_ID」 與TextView的你在列表視圖項XML已經給出了ID(如果沒有給出,給一個)。

1

您在OnItemClickListener中獲得的視圖將返回父佈局,即您的情況爲LinearLayout。

您可以通過以下讓你的TextView:

mListView.setOnItemClickListener(new OnItemClickListener(){ 
     public void onItemClick(AdapterView<?> parent, View view, int position, 
       long id) { 
      TextView txtview = (TextView)view.findViewById(R.id.listTextView); 
      String product = txtview.getText().toString(); 
      Log.v("mSelectedProduct", product); 
     }   
    }); 
相關問題