无法在SQLite dB中解析getContext()

时间:2015-04-27 05:54:41

标签: java android android-sqlite android-contacts

我正在尝试创建一个SQL数据库来存储本地联系信息(主要是电话号码(稍后要验证)和显示名称,以及用户指定的布尔值。我创建了一个新的contactdb类,但是在尝试使用getcontext()时遇到了一些障碍。使用put的辅助值也无法解决。有没有人知道如何解决这个问题?我是否需要为Dbhelper创建一个新的类文件?或者那个位是否意味着在mainactivity类中实例化,其中我希望读取/写入DB的信息?

package treehouse.greenlight;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.provider.BaseColumns;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.*;
import android.os.Bundle;
import android.content.ContentValues;
import android.view.View;

public final class contactdb {
// To prevent someone from accidentally instantiating the contract class,
// give it an empty constructor.
public contactdb() {
}

/* Inner class that defines the table contents */
public static abstract class dbEntry implements BaseColumns {
    public static final String TABLE_NAME = "Contacts";
    public static final String COLUMN_NAME_ENTRY_ID = "entryid";
    public static final Boolean COLUMN_NAME_STATUS = false;
    public static final String COLUMN_NAME_PHONE = "0000000000";
    public static final String COLUMN_NAME_NAME = "N/A";
    private static final String TEXT_TYPE = " TEXT";
    private static final String COMMA_SEP = ",";
    private static final String SQL_CREATE_ENTRIES =
            "CREATE TABLE " + dbEntry.TABLE_NAME + " (" +
                    dbEntry._ID + " INTEGER PRIMARY KEY," +
                    dbEntry.COLUMN_NAME_STATUS + TEXT_TYPE + COMMA_SEP +
                    dbEntry.COLUMN_NAME_NAME + TEXT_TYPE + COMMA_SEP +
                    dbEntry.COLUMN_NAME_PHONE + TEXT_TYPE + COMMA_SEP +// Any other options for the CREATE command
                    " )";

    private static final String SQL_DELETE_ENTRIES =
            "DROP TABLE IF EXISTS " + dbEntry.TABLE_NAME;


    public class DbHelper extends SQLiteOpenHelper {
        DbHelper mDbHelper = new DbHelper(getContext());
        // If you change the database schema, you must increment the database version.
        public static final int DATABASE_VERSION = 1;
        public static final String DATABASE_NAME = "FeedReader.db";

        public DbHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }
        public void onCreate(SQLiteDatabase db) {
            db.execSQL(SQL_CREATE_ENTRIES);


            // Gets the data repository in write mode
            db = mDbHelper.getWritableDatabase();

            // Create a new map of values, where column names are the keys
            ContentValues values = new ContentValues();
            values.put(dbEntry.COLUMN_NAME_ENTRY_ID, id);
            values.put(dbEntry.COLUMN_NAME_STATUS, status);
            values.put(dbEntry.COLUMN_NAME_PHONE, content);

            // Insert the new row, returning the primary key value of the new row
            long newRowId;
            newRowId = db.insert(
                    dbEntry.TABLE_NAME,
                    dbEntry.COLUMN_NAME_NULLABLE,
                    values);
        }

        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // This database is only a cache for online data, so its upgrade policy is
            // to simply to discard the data and start over
            db.execSQL(SQL_DELETE_ENTRIES);
            onCreate(db);
        }

        public void onDowngrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }
    }


}
}

2 个答案:

答案 0 :(得分:3)

您的数据库未从Activity扩展,因此无法访问此方法。 您应该创建从Context接收Activity作为参数的构造函数。你的代码应该是这样的:

private Context ctx;

public contactdb(Context ctx) {
    this.ctx = ctx;
}

在你的Activity中你应该创建这样的实例:

contactdb db = new contactdb(this);

请从大写字母命名您的课程。研究一下java中的代码约定。

答案 1 :(得分:0)

对于SQLiteOpenHelper,

getContext不存在。 Context被传递给构造函数。等到那时创建需要它的变量。

此外,您编写的代码会崩溃 - 它会创建一个实例化新DBHelper对象的无限循环。将Foo name = new Foo()置于Foo对象中将始终崩溃。