2010-08-15 12 views
6

Próbuję otworzyć bazę danych w następujący sposób:Metoda openOrCreateDatabase (String, int, null) jest niezdefiniowane

SQLiteDatabase myDatabase; 
myDatabase = openOrCreateDatabase("sudoku.db", Context.MODE_PRIVATE, null); 

Ten kod działa poprawnie, gdy wdrożyć go w klasie usługi, ale gdy próbuję zaimplementować to w eventhandler onPostExecute klasy GeneraterThread, wdrażanie AsyncTask, pojawia się następujący błąd:

The method openOrCreateDatabase(String, int, null) is undefined for the type GeneraterThread

Odpowiedz

5

Wygląda na to, że próbujesz wywołać metodę openOrCreateDatabase na przykład GeneraterThread który nie ma sposobu (i klasy usługi posiada metody). Prawdopodobnie możesz przekazać odwołanie do obiektu kontekstowego i wywołać metodę na nim. Lub użyj statycznej metody SQLiteDatabase.openOrCreateDatabase().

1

używać go z klasy nadrzędnej

coś

myService.this.openOrCreateDatabase 
5

Wygląda na to, że właśnie ustawiłeś złe argumenty dla funkcji.

w SDK są te definicje:

public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory, DatabaseErrorHandler errorHandler) 

public static SQLiteDatabase openOrCreateDatabase (String path, SQLiteDatabase.CursorFactory factory) 

public static SQLiteDatabase openOrCreateDatabase (File file, SQLiteDatabase.CursorFactory factory) 

Ale wasze powołanie tej funkcji jest źle.

Może chcesz zadzwonić pod numer openDatabase (String path, SQLiteDatabase.CursorFactory factory, int flags)?

W tym przypadku po prostu ustawić argumenty w niewłaściwej kolejności - robisz

openOrCreateDatabase("sudoku.db", Context.MODE_PRIVATE, null); //WRONG 

zamiast:

openDatabase("sudoku.db", null, Context.MODE_PRIVATE); //RIGHT 
+1

Widziałem mnóstwo przykładów online, jak tutaj: http: // sree.cc/google/android/creating-sqlite-database-tables-in-android gdzie openOrCreateDatabase jest wywoływana tak jak Saurabh Verma to nazywa. Jednak dokumentacja online pokazuje tylko te trzy wersje. Jestem zmieszany. – tronman

+2

Właśnie odkryłem, że istnieją dwa różne sposoby dostępu do openOrCreateDatabase ... przez Kontekst http://developer.android.com/reference/android/content/Context.html, który oferuje openOrCreateDatabase (nazwa String, tryb int, SQLiteDatabase.CursorFactory fabryka) i przez SQLiteDatabase http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html, która oferuje metody opisane powyżej. – tronman

+2

Należy jednak zauważyć, że parametr "int flags" w SQLiteDatabase.openDatabase (ścieżka łańcuchowa, fabryka SQLiteDatabase.CursorFactory, flagi int) nie jest równoważny "trybowi int" w metodzie Context openOrCreateDatabase (nazwa String, tryb int, fabryka SQLiteDatabase.CursorFactory). – Anke

Powiązane problemy