Pete N
Pete N

Reputation: 23

Unable to send context, constructor is undefined

I have a DbAdapter class to handle the database stuff in my app, but for some reason I can't call it from a new class I have created (although it works in others).

The code I'm using from the new class is:

DbAdapter mDbHelper = new DbAdapter(this);
        mDbHelper.open();

It doesn't seem to like the "this". The code for my DbAdapter class is:

    public DbAdapter (Context ctx) {
    this.mCtx = ctx;
}

I'm sure this is really stupid question, but if anyone can point me in the right direction it would be much appreciated.

Many thanks,

Pete.

Upvotes: 0

Views: 523

Answers (2)

BuffaloBuffalo
BuffaloBuffalo

Reputation: 7852

What class are you calling

DbAdapter mDbHelper = new DbAdapter(this);
        mDbHelper.open();

from? According to your DbAdapter Constructor definition the parameter must be of type Context. If the class you are calling new DbAdapter(this) from is not the class Context or a subclass of the class Context it is considered invalid code.

Upvotes: 1

Blundell
Blundell

Reputation: 76458

You must be an annoymous inner class therefore this is referening to the Class you are in.

Try

DbAdapter mDbHelper = new DbAdapter(YourActivity.this);

or in your activity

private Context mContext;

...onCreate(){
      mContext = this;
 }

Then were you want it:

DbAdapter mDbHelper = new DbAdapter(mContext );

Upvotes: 1

Related Questions