为什么我收到警告:Class是原始类型.对泛型类型<T>的引用应该参数化"?

hem*_*kar 7 java android

我在收到警告ListActivity.我得到的警告如下所示

  • Class is a raw type. References to generic type Class<T> should be parameterized

它没有造成任何问题,但我想知道为什么我会得到这个警告以及如何抑制它.查看用星号写的行.

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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Ell*_*sch 15

类是通用的,如果你不关心警告你有两个选择使用@SuppressWarnings("rawtypes")或我的偏好使用<?>(这是一个通配符捕获)像这样

Class<?> ourclass = Class.forName("com.app1."+cheese);
Run Code Online (Sandbox Code Playgroud)


Kee*_*san 6

你可以用@SuppressWarnings("rawtypes","unchecked").你也可以制作代码

Class<?> ourclass= Class.forName("com.app1."+cheese);
Run Code Online (Sandbox Code Playgroud)

摆脱警告.现在,您不必使用@SuppressWarnings("rawtypes").编译器期望参数化所有泛型类型


小智 5

要忽略警告“类是原始类型....”在 eclipse* 中执行以下操作:

  1. 单击窗口-首选项-Java-编译器-错误/警告
  2. 点击“通用类型”
  3. 为“原始类型的使用”选择“忽略”
  4. 点击应用
  5. 单击确定
  6. 保存并关闭您的 Eclipse IDE

当您重新打开 eclipse 时,不应再列出这些特定警告。

*对于这个示例解决方案,我使用 Eclipse IDE for Java Developers - 版本:Mars.2 Release (4.5.2)