Android java.lang.ClassCastException: android.widget.LinearLayout 无法转换为 android.widget.TextView

3

我尝试打印在listView中被点击的值,但是我收到了以下异常:

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函数正常工作。这是我的displayList方法:
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代码。 - Shani Goriwal
mTextView 应该是 TextView 类型。 - Sunil Kumar
在 onItemClick 方法中,返回字符串产品的视图会导致类转换异常,请进行检查。 - techieWings
4个回答

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);
        }           
    });

1
使用此代码:
String product = ((TextView) view.findViewById(R.id.listTextView)).getText().toString();

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);
        }           
    });

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”替换为您在列表视图项XML中提供的TextView的ID(如果未提供,请提供一个)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接