0

Reasonably new to Android programming. I get the feeling that I am missing something. This is the error that I get when I try to run the application. This is my code. I'm not seeing the static reference

private void showFullDatabase() {print(StoredWaypointsDB.getAll());}

And in the other Acitivity;

public String[] getAll() {

    ArrayList<String> outputArray = new ArrayList<String>();
    String[] result_columns = new String[]{
            String.valueOf(KEY_LATITUDE), String.valueOf(KEY_LONGITUDE), KEY_NAME};

    String waypointName;
    float latitude;
    float longitude;

    String where = null;
    String whereArgs[] = null;
    String groupBy = null;
    String having = null;
    String order = null;

    SQLiteDatabase db = moduleDBOpenHelper.getWritableDatabase();
    Cursor cursor = db.query(ModuleDBOpenHelper.DATABASE_TABLE,
            result_columns, where,
            whereArgs, groupBy, having, order);
    //
    boolean result = cursor.moveToFirst();
    while (result) {
        waypointName = cursor.getString(cursor.getColumnIndex(KEY_NAME));
        latitude= cursor.getFloat(cursor.getColumnIndex(String.valueOf(KEY_LATITUDE)));
        longitude = cursor.getFloat(cursor.getColumnIndex(String.valueOf(KEY_LONGITUDE)));

        outputArray.add(waypointName + " " + latitude + longitude);
        result = cursor.moveToNext();

    }
    return outputArray.toArray(new String[outputArray.size()]);
}
  • To clarify, the showFullDatabase is in one activity, referencing the getAll method in another activity. The code works perfectly in another application – C. O Ceallaigh Apr 27 '17 at 13:52

1 Answers1

0

Just declare getAll() as static or you won't be able to call that method like that

public static String[] getAll() {
Zeromus
  • 4,472
  • 8
  • 32
  • 40
  • But when I do that it causes an error with moduleDBopenhelper – C. O Ceallaigh Apr 27 '17 at 14:37
  • probably because that moduleDBOpenHelper is a non static field declared in that class... as a general rule you cannot call from a static context any fields or methods which are not static themselves. Read up the duplicate question, it got a detailed explanation about lifecycle and such – Zeromus Apr 27 '17 at 14:45