赞
踩
博客来源:
进行类转换的时候,Integer转换为long是出错
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.demo, PID: 13621
java.lang.ClassCastException: java.lang.Integer cannot be cast to java.lang.Long
at com.example.demo.MainActivity$4.onClick(MainActivity.java:76)
at android.view.View.performClick(View.java:5616)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:967)
at android.view.View$PerformClick.run(View.java:22292)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
我的测试代码为
- Map<String,Integer> map = new HashMap<>();
- map.put("data",1);
- Integer dd = map.get("data");
- long data = map.get("data");
- Long data1 = (long) map.get("data");
- System.out.println(data);
- System.out.println(data1);
- System.out.println((data1==data));
-
- Map<String,Object> map1 = new HashMap<>();
- map1.put("data",dd);
- System.out.println(map1.get("data"));
- //正确转换的代码
- long l = Long.parseLong(map1.get("data").toString());
- //转换会报错的代码
- long l = (long)map1.get("data");
- System.out.println(l);
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。