Android的ORMLite示例无法编译

时间:2011-11-25 06:01:33

标签: java android sqlite ormlite

我一直在努力完成HelloAndroidORMLite示例,但无法成功编译。我遇到了DatabaseHelper类的问题。特别是getDao()方法:

/**
* 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的类型参数;对于带有上限的类型变量D,没有唯一的最大实例com.j256.ormlite.dao.Dao,com.j256.ormlite.dao.Dao

2 个答案:

答案 0 :(得分:7)

尝试使用Netbeans构建我的ormlite项目时出现了类似的错误:

  

将15个源文件编译为〜/ NetBeansProjects / Main / build / classes       Main.java:74: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,并解决了这个问题。

答案 1 :(得分: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());
     }
}