[英]db4o in a class without context android - java
I'm using a db4oHelper in the main activity without problem but when i want use de db in a class without context I've problems.. This class doesn't extends of Activity.. 我在主要活动中使用db4oHelper时没有问题,但是当我想在没有上下文的类中使用de db时我遇到了问题..此类没有Activity的扩展。
public void actualizatrat(Context context){
dbHelper();
db4oHelper.deleteAll();
//...
}
private Db4oHelper dbHelper() {
if (db4oHelper == null) {
db4oHelper = new Db4oHelper(this);
db4oHelper.db();
}
return db4oHelper;
}
the constructor db4oHelper
: 构造函数
db4oHelper
:
public Db4oHelper(Context ctx)
{
context = ctx;
}
Eclipse shows the error: The constructor Db4oHelper
(Actualiza) is undefined Eclipse显示错误:构造函数
Db4oHelper
(Actualiza)未定义
can someone please help me? 有人可以帮帮我吗?
While the class you want to have access to the helper may not be a Context
itself, you likely created it from one of the above components that does, so just pass the Context
you have forward. 虽然您希望访问该帮助程序的类可能不是
Context
本身,但是您可能是通过上述组件之一创建的,因此只需传递您拥有的Context
。
public void actualizatrat(Context context){
dbHelper(context);
db4oHelper.deleteAll();
(...)
}
private Db4oHelper dbHelper(Context context) {
if (db4oHelper == null) {
db4oHelper = new Db4oHelper(context);
db4oHelper.db();
}
return db4oHelper;
}
I suggest getting the Application context instead. 我建议改为获取Application上下文。 Have a look here:
在这里看看:
Static way to get 'Context' on Android? 在Android上获取“上下文”的静态方法?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.