Ich befolge das Android developers Tutorial, um eine Datenbank einzurichten, aber ich habe ein Problem. Das ist meine Schema-Datei:Kann Kontext nicht abrufen und auf Schema von einer Datenbank zugreifen
public final class ContactsDBSchema {
public ContactsDBSchema(){};
public static abstract class ContactsEntry implements BaseColumns
{
public static final String TABLE_NAME = "contacts";
public static final String COLUMN_CONTACT__ID = "contact_id";
public static final String COLUMN_CONTACT__NAME = "contact_name";
public static final String COLUMN_CONTACT__PHONE = "contact_phone";
}
public class ContactsDBhelper extends SQLiteOpenHelper
{
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "contacts.db";
public String SQL_CREATE_ENTRIES = "create table" + ContactsEntry.TABLE_NAME + ContactsEntry.COLUMN_CONTACT__ID +"INTEGER PRIMARY KEY AUTOINCREMENT," + ContactsEntry.COLUMN_CONTACT__NAME + "TEXT," + ContactsEntry.COLUMN_CONTACT__PHONE + "TEX";
public String SQL_DELETE_ENTRIES = "DROP TABLE IF EXISTS" + ContactsEntry.TABLE_NAME ;
public ContactsDBhelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_ENTRIES);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DELETE_ENTRIES);
onCreate(db);
}
public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
onUpgrade(db, oldVersion, newVersion);
}
}
}
Die Frage ist jetzt, wenn ich versuche es wie dieser von einer anderen Klasse zuzugreifen:
public class ContactsDBprocess {
ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(getContext());
}
Ich bin nicht in der Lage richtig die Helfer Klasse zuzugreifen. Die getContext()
Funktion funktioniert nicht und ich erhalte eine Fehlermeldung:
Cannot resolve method
getContext
Ich habe versucht, dies zu tun:
ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(this)
ContactsDBSchema.ContactsDBhelper contactsHelper = new ContactsDBSchema.ContactsDBhelper(getApplicationContext())
Ich habe nicht noch kein Glück. Irgendwelche Vorschläge, wie man dieses Problem löst?
In welcher Klasse möchten Sie 'ContactsDBhelper' instanziieren? Sie müssen einen gültigen 'Kontext' als Konstruktorparameter übergeben. – earthw0rmjim
Ihre Klasse 'ContactDBProcess' muss entweder in einer Klasse mit 'getContext()' definiert werden oder sie muss diese Klasse erweitern. Oder Sie können einen gültigen 'Kontext' an seinen Konstruktor übergeben und dann diese Variable anstelle von 'getContext()' verwenden. – Shaishav