I am trying to compare a double value to another double value inside of a TreeMap object. I am getting the following error at the line where I am comparing myPowerAvg to myMap.get(myMap.get(2)). I’m not sure why I’m getting this error as I’m comparing two doubles (btw, this method is executed within a thread).
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): FATAL EXCEPTION: Thread-23
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): java.lang.ClassCastException: java.lang.String
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): at java.lang.Integer.compareTo(Integer.java:36)
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): at java.util.TreeMap.cmp(TreeMap.java:1317)
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): at java.util.TreeMap.get(TreeMap.java:1282)
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): at com.community.HomeActivity.setTopUsers(HomeActivity.java:159)
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): at com.community.HomeActivity.access$0(HomeActivity.java:136)
12-10 22:12:30.524: ERROR/AndroidRuntime(1059): at com.community.HomeActivity$4.run(HomeActivity.java:127)
code:
if(myPowerAvg < myMap.get(myMap.firstKey())){
TextView view = (TextView)findViewById(R.id.firstPlace);
}else{
TextView view = (TextView)findViewById(R.id.firstPlace);
view.setText("First Place: \n" + myMap.firstKey() + " " +
" at " + myMap.get(myMap.firstKey()) );
}
if(myPowerAvg < myMap.get(myMap.get(2)) && myPowerAvg > myMap.get(myMap.firstKey())){
TextView view = (TextView)findViewById(R.id.secondPlace);
view.setText("Second Place: \n" + AppStatus.mUserName + " " +
" at " + myPowerAvg;
}else{
TextView view = (TextView)findViewById(R.id.secondPlace);
view.setText("Second Place: \n" + myMap.get(2) + " " + " at " +
myMap.get(myMap.get(2)));
}
if(myPowerAvg < myMap.get(myMap.get(2)) && myPowerAvg > myMap.get(myMap.firstKey())){
TextView view = (TextView)findViewById(R.id.secondPlace);
view.setText("Second Place: \n" + AppStatus.mUserName + " " + " at "
+ myPowerAvg);
}else{
TextView view = (TextView)findViewById(R.id.secondPlace);
view.setText("Second Place: \n" + myMap.get(2) + " " + " at " +
myMap.get(myMap.get(2)));
}
You say myMap is a
TreeMap<String, Double>– but you’re calling myMap.get(2). The param to get should be a string.