【问题标题】:Generics compilation error泛型编译错误
【发布时间】:2012-02-23 09:53:07
【问题描述】:

使用 ormlite 的 android 项目中出现错误 我收到此代码的编译错误:

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 ?,?>

但是当我尝试使用 eclipse 构建项目时它工作正常

错误看起来类似于to this SO question

不知道是Idea还是javac的这个bug。

我的配置: IntelliJ IDEA 11.0.2 构建 #IC-111.277 建立在 1 Февраль 2012 г. JDK:1.6.0_29 虚拟机:Java HotSpot(TM) 64 位服务器虚拟机 供应商:Apple Inc.

【问题讨论】:

  • 我想看看DatabaseHelper.getDao()方法。
  • 当然!你可以从ormlite.com下载源代码
  • 但是没有名为 DatabaseHelper 的类。
  • 这是android包的一部分

标签: java android generics intellij-idea ormlite


【解决方案1】:

我在 Eclipse 中没有收到此错误,但我知道为什么您会看到问题。 mdao 定义为 Dao&lt;Object, Integer&gt;,但您正在调用 getDao(mClass),其中 mclassClass&lt;?&gt;。对象!=?在普通土地上。

你可以把你的整个类变成一个泛型类型。像下面这样的东西会起作用。

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);
        }
    }
}

应该可以的。

【讨论】:

  • 谢谢。我唯一不明白的是,为什么Idea不能编译这个,但是eclipse cat
  • 不知道。在这种情况下,Eclipse 可能会更加宽容。
【解决方案2】:

就我而言,我可以使用getDataDao() 而不是getDao() 来规避这个问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-21
    • 1970-01-01
    • 1970-01-01
    • 2012-12-15
    • 2020-03-04
    相关资源
    最近更新 更多