无法解析方法'makeText(Data.DataBaseHandler,java.lang.String,int)'

时间:2017-08-16 20:51:40

标签: android toast

请检查一下,有两个问题,第一个是这个数据库没有创建,第二个是这个toast给了我这个错误。

 public static final String KEY_ID="id";
public static final String KEY_NAME="Name";
public static final String KEY_IMAGE = "levelone";

public DataBaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID +
            " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_IMAGE + " BLOB" + ")";
    sqLiteDatabase.execSQL(CREATE_TABLE);

    Toast.makeText(DataBaseHandler.this,"sadads",Toast.LENGTH_LONG).show();

        }


@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    onCreate(sqLiteDatabase);
}

1 个答案:

答案 0 :(得分:0)

  
      
  1. 数据库没有创建原因?
  2.   

首次拨打getWritableDatabase()getReadableDatabase()时创建的数据库。因为DataBaseHandler只是创建帮手。

DataBaseHandler helper = new DataBaseHandler(context);
helper.getWritableDatabase();
  
      
  1. Toast没有显示原因?
  2.   

Toast需要显示上下文。您不能使用SqliteOpenHelper DataBaseHandler作为上下文。但是您可以将构造函数中传递的上下文用于getApplicationContext()

public static final String KEY_ID = "id";
public static final String KEY_NAME = "Name";
public static final String KEY_IMAGE = "levelone";
private Context context;

public DataBaseHandler(Context context) {
    super(context, DATABASE_NAME, null, DATABASE_VERSION);
    this.context = context;
}

@Override
public void onCreate(SQLiteDatabase sqLiteDatabase) {

    String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "(" + KEY_ID +
            " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT," + KEY_IMAGE + " BLOB" + ")";
    sqLiteDatabase.execSQL(CREATE_TABLE);

    Toast.makeText(context.getApplicationContext(), "sadads", Toast.LENGTH_LONG).show();

}


@Override
public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);

    onCreate(sqLiteDatabase);
}