5

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

Bryanyan
  • 677
  • 3
  • 13
  • 30

2 Answers2

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

edthethird
  • 6,263
  • 2
  • 24
  • 34
  • if like this how to call updateLocation() from a method of another separate class. The class is public class SMSReceiver extends BroadcastReceiver { }. – Bryanyan Apr 06 '13 at 01:46
  • from SMSReceiver class, can I call like DetailMapView.updateLocation(context, tel, msg); – Bryanyan Apr 06 '13 at 01:49
  • What is the logic behind? Where can I find the info? – Bryanyan Apr 06 '13 at 02:26
  • 2
    This is a pretty common concept. In order to keep `static` methods stateless, the implication is that any state (such as context, number, etc.) must be passed in as parameters. – edthethird Apr 06 '13 at 19:42
  • This sounds logic. Thank you! :) I think I understand now a lot better how android works... (even the complete object orientated programming concept seem to be a bit clearer for me) – Martin Pfeffer Sep 30 '14 at 08:34
  • omg I feel that I am a moron!! What an easy and sensible solution and I didn't think this! – MareCieco Dec 05 '17 at 00:44
0

Your function 'updateLocation' is static. This means that there is no instance associated with it, and therefor no 'this'.

You are going to need to find another way to get the context for your database helper class.

Edit: personally, I find it a pain to have to pass context around to so many different functions. In activities, you will just use activity as your context, but in many cases (such as this one, with database) you will want to use application context. For that, I find this method helpful:
Static way to get 'Context' on Android?

Community
  • 1
  • 1
Tom
  • 17,103
  • 8
  • 67
  • 75
  • Yeah I am new to Android and I am not clear what is about Context. Any good reading for Context. – Bryanyan Apr 06 '13 at 01:48
  • I added a bit more about how you can handle context in your app. – Tom Apr 06 '13 at 02:07
  • check out this link, maintaining a static reference to a context on Android isn't a good idea unless you are EXTREMELY careful: http://android-developers.blogspot.com/2009/01/avoiding-memory-leaks.html – edthethird Apr 06 '13 at 19:48
  • 1
    That's about the dangers of memory leaks, particularly when you hold an activity context. In this case, we are talking about holding (and accessing) the app context - I think the issues are different. – Tom Apr 06 '13 at 21:19