Android java.lang.ClassCastException:无法将android.widget.LinearLayout强制转换为android.widget.TextView [英] Android java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView

查看:105
本文介绍了Android java.lang.ClassCastException:无法将android.widget.LinearLayout强制转换为android.widget.TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试打印已在listView中单击的值,但是随后出现以下异常:

I'm trying to print the value that has been clicked on in the listView, but then i'm getting the following exception:

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方法:

The createList function is working properly. Here is my displayList method:

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代码是:

My xml code for the file activity_home_screen.xml is:

<?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" />  

任何帮助都是有用的!

谢谢.

推荐答案

您可以尝试

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

这篇关于Android java.lang.ClassCastException:无法将android.widget.LinearLayout强制转换为android.widget.TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆