I have a problem in initializing a new object using this in a static method. I have a database class like follow.
public class LatLogDBAdapter {
private final Context mCtx;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
Log.w(TAG, DATABASE_CREATE);
db.execSQL(DATABASE_CREATE);
}
}
I have a static method in a another class, in that static method I like to initialize DatabaseHelper using this Context, but the error is "Can't use this in a static context".
My static method in a separate class is as follow,
public class DetailMapView extends FragmentActivity {
public static void updateLocation(String number, String LatLong){
LatLogDBAdapter dbHelper = new LatLogDBAdapter(this);
}
}
How can I do it not to have error of "Can't use this in a static context". Thanks
public class DetailMapView extends FragmentActivity {
public static void updateLocation(Context context, String number, String LatLong){
LatLogDBAdapter dbHelper = new LatLogDBAdapter(context);
}
}
Add a Context
as a parameter to your static method, and pass it in when you call the method.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With