Android ClassCastException 与 TreeMap 比较
我正在尝试将双精度值与 TreeMap 对象内的另一个双精度值进行比较。我在将 myPowerAvg 与 myMap.get(myMap.get(2)) 进行比较的行中收到以下错误。我不确定为什么在比较两个双精度值时会收到此错误(顺便说一句,此方法是在线程内执行的)。
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)
代码:
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)));
}
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)));
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您说 myMap 是一个
TreeMap
- 但您正在调用 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.