使用Singleton从多个活动访问数据库时无法解析符号“上下文”

时间:2015-12-01 03:01:28

标签: android android-studio singleton sqliteopenhelper

我一直在尝试使用YouTube上的教程在我的应用程序中实现数据库的使用:https://www.youtube.com/watch?v=T0ClYrJukPA

但遇到了尝试从多个活动访问我的数据库的问题。 Stack上的一个问题:How to pass database objects between activities (Android)

带我到这个:http://www.androiddesignpatterns.com/2012/05/correctly-managing-your-sqlite-database.html看起来它会正常工作,除了我试图使用数据库的主要活动中的错误“无法解析符号'上下文'。任何帮助都将是非常感激。

我的主要活动:

import android.content.Context;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Spinner;

public class Home extends AppCompatActivity {
    DatabaseHelper myDb;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
       //create database

        myDb = DatabaseHelper.getInstance(context);

我的DatabaseHelper:

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;


public class DatabaseHelper extends SQLiteOpenHelper {

    private static DatabaseHelper sInstance;

    public static final String DATABASE_NAME = "character_info.db";
    public static final String TABLE_NAME = "characters_table";
    public static final String COL_1 = "NAME";
    public static final String COL_3 = "HEALTH";
    public static final String COL_2 = "STRENGTH";
    public static final String COL_4 = "ENDURANCE";
    public static final String COL_5 = "CARRY_WEIGHT";
    public static final String COL_6 = "AGILITY";
    public static final String COL_7 = "INTELLIGENCE";
    public static final String COL_8 = "CHARISMA";
    public static final String COL_9 = "ZPREP";
    public static final String COL_10 = "INVENTORY";

    public static synchronized DatabaseHelper getInstance(Context context) {

        if (sInstance == null) {
            sInstance = new DatabaseHelper(context.getApplicationContext());
        }
        return sInstance;
    }

    private DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, 1);

    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        String SQL_String = "create table " + TABLE_NAME +" (NAME TEXT PRIMARY KEY, HEALTH TEXT, STRENGTH TEXT" +
                ", ENDURANCE TEXT, CARRY_WEIGHT TEXT, AGILITY TEXT, INTELLIGENCE TEXT, CHARISMA TEXT, ZPREP TEXT, INVENTORY TEXT)";

        db.execSQL(SQL_String);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }

    public boolean insertData(String name, String health, String strength, String endurance, String carryweight,
                              String agility, String intelligence, String charisma, String zprep, String inventory) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues contentValues = new ContentValues();
        contentValues.put(COL_1, name);
        contentValues.put(COL_2, health);
        contentValues.put(COL_3, strength);
        contentValues.put(COL_4, endurance);
        contentValues.put(COL_5, carryweight);
        contentValues.put(COL_6, agility);
        contentValues.put(COL_7, intelligence);
        contentValues.put(COL_8, charisma);
        contentValues.put(COL_9, zprep);
        contentValues.put(COL_10, inventory);
        long result = db.insert(TABLE_NAME, null, contentValues);

        if (result == -1)
            return false;
        else
            return true;

    }
}

1 个答案:

答案 0 :(得分:2)

更改

myDb = DatabaseHelper.getInstance(context);

myDb = DatabaseHelper.getInstance(getApplicationContext());