2012-02-23 87 views
2

錯誤出現在Android項目使用ormlite 我得到的編譯錯誤在此代碼:泛型編譯錯誤

public class DatabaseModel { 
     private Dao<Object, Integer> mDao = null; 
     private DatabaseHelper mHelper; 
     private Class<?> mClass; 

     public DatabaseModel(DatabaseHelper h, Class<?> c) { 
       mHelper = h; 
       mClass = c; 
       try { 
         mDao = mHelper.getDao(mClass); 
       } catch (SQLException e) { 
         Debug.e("Can't get dao", e.getStackTrace()); 
         throw new RuntimeException(e); 
       } 
     } 

on line 25 mDao = mHelper.getDao(mClass); 

Error: type parameters of <D>D cannot be determined; no unique maximal 
instance exists for type variable D with upper bounds 
    com.j256.ormlite.dao.Dao<java.lang.Object,java.lang.Integer>, 
    com.j256.ormlite.dao.Dao<capture#296 of ?,?> 

但是,當我試圖建立使用項目偏食,它工作正常

錯誤類似於to this SO question

我不知道Idea或javac的這個bug。

我的配置: 的IntelliJ IDEA 11.0.2 構建#IC-111.277 內置1個Февраль2012г. JDK:1.6.0_29 VM:Java的熱點(TM)64位服務器VM 廠商:蘋果公司

+0

我想看一下'DatabaseHelper.getDao()'方法。 – Malcolm 2012-02-23 10:36:27

+0

ofcourse!你可以從http://ormlite.com/ – 2012-02-23 11:36:24

+0

下載源代碼但是沒有名爲DatabaseHelper的類。 – Malcolm 2012-02-23 14:23:39

回答

1

我沒有收到在Eclipse這個錯誤,但我可以看到你爲什麼會看到一個問題。 mdao定義爲Dao<Object, Integer>,但您致電getDao(mClass)其中mclassClass<?>。對象!=?在通用土地上。

你可以把你的整個類變成一個泛型類型。像下面的東西會起作用。

public class DatabaseModel<T, ID> { 
    private Dao<T, ID> mDao = null; 
    private DatabaseHelper mHelper; 
    private Class<T> mClass; 

    public DatabaseModel(DatabaseHelper h, Class<T> c) { 
     mHelper = h; 
     mClass = c; 
     try { 
      mDao = mHelper.getDao(mClass); 
     } catch (SQLException e) { 
      Debug.e("Can't get dao", e.getStackTrace()); 
      throw new RuntimeException(e); 
     } 
    } 
} 

這應該工作。

+0

謝謝。唯一我不明白的是,爲什麼Idea不能編譯這個,但是eclipse貓 – 2012-02-23 21:57:24

+0

不知道。在這種情況下,Eclipse可能更加寬鬆。 – Gray 2012-02-23 21:58:51

0

就我而言,我可以通過使用getDataDao()而不是getDao()來規避問題。