Java 为什么我收到警告:Class is a raw type。对泛型类型 Class<T> 的引用应该被参数化”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20451096/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"?
提问by hemkar
I am getting warning in my ListActivity
. The warning I am getting is shown below
我的ListActivity
. 我收到的警告如下所示
Class is a raw type. References to generic type Class<T> should be parameterized
Class is a raw type. References to generic type Class<T> should be parameterized
It is not creating any problems, but I would like to know why I am getting this warning and how to suppress it. See line which written within asterisks.
它不会造成任何问题,但我想知道为什么会收到此警告以及如何抑制它。见星号内写的行。
public class Menu extends ListActivity {
String classes[]={"Second","example1","example2","example3","example4"};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(Menu.this,android.R.layout.simple_list_item_1,classes));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
String cheese=classes[position];
try{
**Class ourclass= Class.forName("com.app1."+cheese);**
Intent ourintent= new Intent(Menu.this,ourclass);
startActivity(ourintent);
}catch(ClassNotFoundException e){
e.printStackTrace();
}
}
}
回答by Keerthivasan
You can use @SuppressWarnings("rawtypes","unchecked")
. You can also make the code
您可以使用@SuppressWarnings("rawtypes","unchecked")
. 您也可以制作代码
Class<?> ourclass= Class.forName("com.app1."+cheese);
to get rid of the warning. Now, you don't have to use @SuppressWarnings("rawtypes")
. Compiler expects all the generic types to be parameterized
摆脱警告。现在,您不必使用@SuppressWarnings("rawtypes")
. 编译器期望所有泛型类型都被参数化
回答by Elliott Frisch
Class is generic, if you don't care for the warning you have two choices use @SuppressWarnings("rawtypes")
or my preference use the <?>
(that is a wildcard capture) like this
类是通用的,如果您不关心警告,您有两种选择使用@SuppressWarnings("rawtypes")
或我的偏好使用<?>
(即通配符捕获)这样的
Class<?> ourclass = Class.forName("com.app1."+cheese);
回答by Mark Burleigh
To ignore the warning "Class is a raw type...." do the following inside eclipse*:
要忽略警告“类是原始类型....”在 eclipse* 中执行以下操作:
- Click Window-Preferences-Java-Compiler-Errors/Warnings
- Click "Generic Types"
- Choose "Ignore" for "Usage of a Raw Type"
- Click Apply
- Click OK
- Save and close your eclipse IDE
- 单击窗口-首选项-Java-编译器-错误/警告
- 点击“通用类型”
- 为“原始类型的使用”选择“忽略”
- 点击应用
- 单击确定
- 保存并关闭您的 Eclipse IDE
When you reopen eclipse, these specific warnings should no longer be listed.
当您重新打开 eclipse 时,不应再列出这些特定警告。
*For this example solution I'm using Eclipse IDE for Java Developers - Version: Mars.2 Release (4.5.2)
*对于这个示例解决方案,我使用 Eclipse IDE for Java Developers - 版本:Mars.2 Release (4.5.2)