2011-11-25 50 views
7

我一直試圖通過HelloAndroid示例工作ORMLite,但一直未能成功編譯。我遇到了DatabaseHelper類的問題。具體地,getDao()方法:Android的ORMLite示例不會編譯

/** 
* Returns the Database Access Object (DAO) for our SimpleData class. 
* It will create it or return the cached value. 
*/ 
public Dao<SimpleData, Integer> getDao() throws SQLException { 
    if (simpleDao == null) { 
    simpleDao = getDao(SimpleData.class); 
    } 
    return simpleDao; 
} 

這裏是我接收編譯時間錯誤:

d類型參數不能被確定;沒有唯一的最大實例存在與上界com.j256.ormlite.dao.Dao類型的變量d,com.j256.ormlite.dao.Dao

+4

這看起來很像下面的[bug](https://bugs.eclipse.org/bugs/show_bug.cgi?id=98379)。編譯工作在eclipse中,但由於類型推斷問題而不能用普通的java編譯器。你如何編寫代碼? – CamilleLDN

+1

我使用的是運行在Ubuntu 10中的JetBrains的IntelliJ IDE。 – curtisthibault

+0

我同意你@Mademoiselle Geek(酷酷的名字)。嘿curtisthibault,你在Ubuntu上使用什麼版本的Java。看起來這是固定在6u24-rev(b22)和6u25(b01)。 – Gray

回答

7

15個編譯源文件到〜/的NetBeansProjects /主/編譯/類 Main.java:74:嘗試使用NetBeans構建ormlite項目時,類似的錯誤d的類型參數不能確定; 對於帶有上限的類型變量D,沒有唯一的最大實例 com.j256.ormlite.dao.Dao,com.j256.ormlite.dao.Dao pcDao = DaoManager.createDao(connectionSource,PC.class);

由於評論的原因,我將Java平臺從OpenJDK 1.6切換到Oracle的JDK 1.7.0_02,並解決了問題。

0

我的解決辦法:

public class HelloAndroid extends OrmLiteBaseActivity<DatabaseHelper> 
{ 
    private final String LOG_TAG = getClass().getSimpleName(); 

    /** 
    * Called when the activity is first created. 
    */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     Log.i(LOG_TAG, "creating " + getClass() + " at " + System.currentTimeMillis()); 
    TextView tv = new TextView(this); 
     doSampleDatabaseStuff("onCreate", tv); 
     setContentView(tv); 
    } 

    /** 
    * Do our sample database stuff. 
    */ 
    private void doSampleDatabaseStuff(String action, TextView tv) 
    { 
     // get our dao 
     RuntimeExceptionDao<SimpleData, Integer> simpleDao = getHelper().getSimpleDataDao(); 
     // query for all of the data objects in the database 
     List<SimpleData> list = simpleDao.queryForAll(); 
     // our string builder for building the content-view 
     StringBuilder sb = new StringBuilder(); 
     sb.append("got ").append(list.size()).append(" entries in ").append(action).append("\n"); 

     // if we already have items in the database 
     int simpleC = 0; 
     for (SimpleData simple : list) 
     { 
      sb.append("------------------------------------------\n"); 
      sb.append("[").append(simpleC).append("] = ").append(simple).append("\n"); 
      simpleC++; 
     } 
     sb.append("------------------------------------------\n"); 
     for (SimpleData simple : list) 
     { 
      simpleDao.delete(simple); 
      sb.append("deleted id ").append(simple.id).append("\n"); 
      Log.i(LOG_TAG, "deleting simple(" + simple.id + ")"); 
      simpleC++; 
     } 

     int createNum; 
     do 
     { 
      createNum = new Random().nextInt(3) + 1; 
     } 
     while (createNum == list.size()); 
     for (int i = 0; i < createNum; i++) 
     { 
      // create a new simple object 
      long millis = System.currentTimeMillis(); 
      SimpleData simple = new SimpleData(millis); 
      // store it in the database 
      simpleDao.create(simple); 
      Log.i(LOG_TAG, "created simple(" + millis + ")"); 
      // output it 
      sb.append("------------------------------------------\n"); 
      sb.append("created new entry #").append(i + 1).append(":\n"); 
      sb.append(simple).append("\n"); 
      try 
      { 
       Thread.sleep(5); 
      } 
      catch (InterruptedException e) 
      { 
       // ignore 
      } 
     } 
     tv.setText(sb.toString()); 
     Log.i(LOG_TAG, "Done with page at " + System.currentTimeMillis()); 
    } 
} 
+4

解決方案是什麼?你在這個例子中改變了什麼,以回答@ curtisthibault的問題?你能再解釋一下嗎? – Gray