简体   繁体   English

Java程序在eclipse中编译而不是netbeans?

[英]Java program compiles in eclipse but not netbeans?

In eclipse this line of code compiles and the program runs fine. 在eclipse中,这行代码编译并且程序运行正常。

SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), (List<? extends Map<String, ?>>) data, R.layout.message_list_item, from, to);

It gives the warning: 它给出了警告:

unchecked cast from ArrayList<Map> to List<? extends Map<String, ?>>

In netbeans however: 但是在netbeans中:

Incompatible types: ArrayList<Map> cannot be converted to List<? extends Map<String, ?>>

And then the runtime error: 然后是运行时错误:

error: inconvertible types
            SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), (List<? extends Map<String, ?>>) data, R.layout.message_list_item, from, to);
required: List<? extends Map<String,?>>
found:    ArrayList<Map>

Can someone please explain this difference in behaviour to me? 有人可以向我解释这种行为差异吗? Thanks. 谢谢。

检查两个平台并确保在两个IDE中都运行相同的JDK版本,这很可能是问题所在。

Eclipse has it's own Java compiler while Netbeans uses the one from your JDK. Eclipse拥有自己的Java编译器,而Netbeans使用JDK中的编译器。 They are compatible and both produce valid code but for some obscure features (especially in the area of generics), they aren't 100% compatible. 它们是兼​​容的并且都产生有效的代码但是对于一些不起眼的特征(特别是在泛型领域),它们不是100%兼容的。 Most of the time, the Eclipse compiler will be able to handle more complex cases. 大多数情况下,Eclipse编译器将能够处理更复杂的情况。

Try to install Java 7 and use that for Netbeans. 尝试安装Java 7并将其用于Netbeans。 Oracle has fixed some bugs in the compiler. Oracle修复了编译器中的一些错误。

EDIT After looking at the question again, I see a problem: You're mixing generic and raw types: You try to cast Map to Map<String,?> . 编辑再次查看问题后,我发现了一个问题:您正在混合泛型和原始类型:您尝试将MapMap<String,?>

Try to use a generic type when you declare data instead of ArrayList<Map> . 在声明data而不是ArrayList<Map>时尝试使用泛型类型。 If that doesn't work, then you can use this workaround: 如果这不起作用,那么您可以使用此解决方法:

    @SuppressWarnings( { "unchecked", "rawtypes" } )
    java.util.List<? extends Map<String, ?>> tmp = (java.util.List<? extends Map<String, ?>>)((List) data);

This little trick strips all generics information from the right hand side before the compiler has a chance to use it against you. 在编译器有机会对您使用它之前,这个小技巧会从右侧删除所有泛型信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM