簡體   English   中英

為什么我收到警告:Class是原始類型。對泛型類型<T>的引用應該參數化“?

[英]Why am I getting the warning :Class is a raw type. References to generic type Class<T> should be parameterized"?

我在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();
        }
    }
}

類是通用的,如果你不關心警告你有兩個選擇使用@SuppressWarnings("rawtypes")或者我的偏好使用<?> (這是一個通配符捕獲 )這樣的

Class<?> ourclass = Class.forName("com.app1."+cheese);

您可以使用@SuppressWarnings("rawtypes","unchecked") 你也可以制作代碼

Class<?> ourclass= Class.forName("com.app1."+cheese);

擺脫警告。 現在,您不必使用@SuppressWarnings("rawtypes") 編譯器期望參數化所有泛型類型

要忽略警告“類是原始類型......”,請在eclipse *中執行以下操作:

  1. 單擊Window-Preferences-Java-Compiler-Errors / Warnings
  2. 點擊“通用類型”
  3. 為“原始類型的使用”選擇“忽略”
  4. 單擊“應用”
  5. 單擊確定
  6. 保存並關閉eclipse IDE

當您重新打開eclipse時,不應再列出這些特定警告。

*對於此示例解決方案,我正在使用Eclipse IDE for Java Developers - Version:Mars.2 Release(4.5.2)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM