gpt4 book ai didi

java - Android ClassCastException 与 TreeMap 比较

转载 作者:行者123 更新时间:2023-12-01 05:34:08 25 4
gpt4 key购买 nike

我正在尝试将一个 double 值与 TreeMap 对象内的另一个 double 值进行比较。我在将 myPowerAvg 与 myMap.get(myMap.get(2)) 进行比较的行中收到以下错误。我不确定为什么在比较两个 double 值时会收到此错误(顺便说一句,此方法是在线程内执行的)。

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)));
}

最佳答案

你说 myMap 是 TreeMap<String, Double> - 但你正在调用 myMap.get(2)。要获取的参数应该是一个字符串。

关于java - Android ClassCastException 与 TreeMap 比较,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8461779/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com