我正在尝试打印ListVIEw中单击的值,但后来我得到以下异常:
07-04 10:40:56.482: E/AndroIDRuntime(1356): FATAL EXCEPTION: main07-04 10:40:56.482: E/AndroIDRuntime(1356): java.lang.classCastException: androID.Widget.linearLayout cannot be cast to androID.Widget.TextVIEw07-04 10:40:56.482: E/AndroIDRuntime(1356): at com.passwordkeeper.ui.ActivityHomeScreen.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" />
任何帮助都会有用!
谢谢.
解决方法:
你可以试试这个
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全部内容,希望文章能够帮你解决Android java.lang.ClassCastException:android.widget.LinearLayout无法强制转换为android.widget.TextView所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)