简体   繁体   中英

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.

It means that parentAdapter.getItemAtPosition(position) is returning a HashMap instance, which you're trying to convert to a string with the line

String selectedRouteName = (String) parentAdapter.getItemAtPosition(position);

You should check the content of that AdaptaverView .

Please compare the java with real life, you are trying to convert DOG object into Peacock object , that is not possible.

And you should use toString() method of object class for string conversion.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM