这是代码。
public class DatabaseManager extends android.app.Activity
{
public SQLiteDatabase mydatabase ;
private static final Object MODE_PRIVATE = null;
public static final String TABLE_NAME = "Fests";
// this function is called first.
public void createDatabase()
{
try{
mydatabase = openOrCreateDatabase("fests",SQLiteDatabase.CREATE_IF_NECESSARY,null);
System.out.println("no probs with open function.");
mydatabase.execSQL("CREATE TABLE IF NOT EXISTS Fests (FestId VARCHAR,Festname VARCHAR);");
}catch(Exception e)
{
Log.d("creating db","exception caught."+e);
}
}
我在第一行本身得到空指针异常,即openOrCreateDatabase函数,无法弄清楚原因。 我是android编程的新手。请帮帮我。
答案 0 :(得分:1)
我认为您正在使用public class DatabaseManager extends android.app.Activity
这是错误的更改为public class DatabaseManager extends SQLiteOpenHelper
答案 1 :(得分:0)
如上所述,改变你的课程延伸。
您也不需要变量mydatabase
。班级中已有一个名为database
的人。它将为您提供它的创建,如果您提供名称&版。
E.g。
private static final String DATABASE_NAME = "Name.db";
private static final int DATABASE_VERSION = 2;
/**
* this gets called automatically on opening the database
*
* @param context
*/
public DatabaseManager(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// if the database does not exist this method is called to create it
@Override
public void onCreate(SQLiteDatabase database) {
for (String CREATE_TABLE : CREATE_TABLES)
{
database.execSQL(CREATE_TABLE);
}
}
// when the version changes this method updates the version number and
// creates a log entry
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(SQLiteHelper.class.getName(), "Upgrading database from version "
+ oldVersion + " to " + newVersion
+ ", which will destroy all old data");
for (String TABLE: TABLES)
{
db.execSQL("DROP TABLE IF EXISTS " + TABLE);
}
onCreate(db);
}