[英]Android java.util.HashMap cannot be cast to java.lang.String
public void onItemClick(AdapterView<?> parentAdapter, View view, int position,
long id) {
TextView clickedView = (TextView) view;
/* Line 79 */ String selectedRouteName = (String) parentAdapter.getItemAtPosition(position);
String temp = selectedRouteName.substring(0, 2);
Error shown is: 显示的错误是:
02-18 01:56:53.676: E/AndroidRuntime(821): java.lang.ClassCastException: java.util.HashMap cannot be cast to java.lang.String
02-18 01:56:53.676: E/AndroidRuntime(821): at com.coltonnicotera.londontransitguide.SchedulesActivity$1.onItemClick(SchedulesActivity.java:79)
Not sure what is causing this, I found java.lang.String cannot be cast to java.util.HashMap exception and it seems that I'm doing everything right. 不知道是什么原因造成的,我发现java.lang.String无法强制转换为java.util.HashMap异常 ,而且看来我所做的一切都正确。
It means that parentAdapter.getItemAtPosition(position)
is returning a HashMap instance, which you're trying to convert to a string with the line 这意味着
parentAdapter.getItemAtPosition(position)
返回一个HashMap实例,您正在尝试将其转换为带有以下内容的字符串:
String selectedRouteName = (String) parentAdapter.getItemAtPosition(position);
You should check the content of that AdaptaverView . 您应该检查那个AdaptaverView的内容。
Please compare the java with real life, you are trying to convert DOG object into Peacock object , that is not possible. 请将Java与现实生活进行比较,您正在尝试将DOG对象转换为Peacock对象,这是不可能的。
And you should use toString()
method of object class for string conversion. 并且您应该使用对象类的
toString()
方法进行字符串转换。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.