简体   繁体   中英

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..

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 :

    public Db4oHelper(Context ctx)
    {
          context = ctx;
    }

Eclipse shows the error: The constructor Db4oHelper (Actualiza) is undefined

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.

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. Have a look here:

Static way to get 'Context' on Android?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM