getContext() 无法在 SQLite DB 中解析

getContext() cannot be resolved in SQLite dB

我正在尝试创建一个 SQL 数据库来存储本地联系信息(主要是 phone#(稍后验证)和显示名称,以及用户-指定的布尔值。我创建了一个新的 contactdb class,但是在尝试使用 getcontext() 时我 运行 遇到了一些问题。put 的次要值也无法解析。有没有人有任何提示如何解决这个问题?我是否需要为 Dbhelper 创建一个新的 class 文件?或者该位是否要在主活动 class 中实例化,其中我希望读取/写入信息到数据库?

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);
        }
    }


}
}

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

此外,您编写的代码会崩溃 - 它会创建实例化新 DBHelper 对象的无限循环。将 Foo name = new Foo() 放在 Foo 对象中总是会崩溃。

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

private Context ctx;

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

并且在您的 Activity 中,您应该像这样创建实例:

contactdb db = new contactdb(this);

请用大写字母命名您的 类。稍微研究一下 java.

中的代码约定