Android ClassCastException 与 TreeMap 比较

发布于 2024-12-20 12:15:27 字数 2277 浏览 2 评论 0原文

我正在尝试将双精度值与 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

去了角落 2024-12-27 12:15:27

您说 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文