泛型编译错误

发布于 2025-01-07 23:22:32 字数 1247 浏览 1 评论 0原文

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

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

该错误看起来类似 这个问题

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

我的配置: IntelliJ IDEA 11.0.2 构建#IC-111.277 建于 2012 年 1 月 1 日。 JDK:1.6.0_29 VM:Java HotSpot(TM) 64 位服务器 VM 供应商:苹果公司

Error appears in android project using ormlite
I get compilation error on this code:

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

But when i tries to build project using eclipse it works fine

The error looks similar to this SO question.

I don't know whether this bug of Idea or javac.

My configuration:
IntelliJ IDEA 11.0.2
Build #IC-111.277
Built on 1 Февраль 2012 г.
JDK: 1.6.0_29
VM: Java HotSpot(TM) 64-Bit Server VM
Vendor: Apple Inc.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

七七 2025-01-14 23:22:32

我在 eclipse 中没有收到此错误,但我可以明白为什么您会看到问题。 mdao 定义为 Dao 但您正在调用 getDao(mClass),其中 mclass。对象!=?在通用土地上。

您可以将整个类转变为泛型类型。像下面这样的东西会起作用。

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

那应该有效。

I'm not getting this error in eclipse but I can see why you would see a problem. mdao is defined as Dao<Object, Integer> but you are calling getDao(mClass) where mclass is a Class<?>. Object != ? in generic land.

You could turn your entire class into a generic type. Something like the following would work.

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

That should work.

最后的乘客 2025-01-14 23:22:32

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

In my case, I could circumvent the problem by using getDataDao() instead of getDao().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文