我試圖打印已在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" />
任何幫助將是有益的!
謝謝。
在這裏發表您的XML代碼.. –
mTextView應該是TextView的類型 –
視圖返回文本字符串作爲產品,在onItemClick方法是給你classcast錯誤時拋出,檢查它。 – techieWings