我正在尝试从数据库中获取值,但是我得到了Exception无法实例化活动componentinfo我在清单文件中提到了所有活动名称的问题是什么,但仍然强行关闭。
public class DataBaseCreator {
private static final String DATABASE_NAME = "SetMessage.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_TABLE = "Set_message";
public static final String KEY_ROWID = "_id";
public static final String KEY_MESSAGE = "Message";
public static final String KEY_TITTLE = "Tittle";
public static final String KEY_DATEFROM = "Date_From";
public static final String KEY_STARTTIME= "Start_Time";
public static final String KEY_EXPIRYDATE="Expiry_Date";
public static final String KEY_EXPIRYTIME="Expiry_Time";
private Context context;
private SQLiteDatabase db;
private OpenHelper mDbHelper;
private SQLiteStatement insertStmt;
public DataBaseCreator(Context context) {
this.context = context;
OpenHelper openHelper = new OpenHelper(this.context);
this.db = openHelper.getWritableDatabase();
}
public long insert(String TABLE_NAME, String TABLE_FIELDS, String TABLE_VALUES) {
String INSERT = "insert into "
+ TABLE_NAME + "("+ TABLE_FIELDS +") values ("+TABLE_VALUES+")";
this.insertStmt = this.db.compileStatement(INSERT);
return this.insertStmt.executeInsert();
}
public void closeDb() {
this.db.close();
}
private static class OpenHelper extends SQLiteOpenHelper {
OpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Set_message (Tittle VARCHAR(30) NOT NULL," +
"Message VARCHAR(30) NOT NULL,"+
"Date_from DATETIME NOT NULL,"+
"Start_Time DATETIME NOT NULL,"+
"Expiry_Date DATETIME NOT NULL,"+
"Expiry_Time DATETIME NOT NULL)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP DATABASE IF EXISTS " + DATABASE_NAME);
onCreate(db);
}
}
public void open() {
mDbHelper = new OpenHelper(context);
db = mDbHelper.getWritableDatabase();
}
public void close() {
mDbHelper.close();
}
public Cursor fetchRecords(long id) throws SQLException {
Cursor mCursor =
db.query(true, DATABASE_TABLE, new String[] { KEY_ROWID,
KEY_MESSAGE, KEY_TITTLE,KEY_DATEFROM ,KEY_STARTTIME,KEY_EXPIRYDATE,KEY_EXPIRYTIME}, KEY_ROWID + "=" + id, null,
null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
}
&安培;我试图获取vales的主要活动是
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
baseCreator.open();
Cursor c=baseCreator.fetchRecords(1);
Toast.makeText(this, "Tittle: " + c.getString(1) + " Message: "+ c.getString(2)+ " Date_from: "+ c.getString(3)+ " Start_Time: "+ c.getString(4)+ " Expiry_Date: "+ c.getString(5)+ " Expiry_Time: "+ c.getString(6), Toast.LENGTH_LONG).show();
baseCreator.close();
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.setdetails);
答案 0 :(得分:2)
在打开之前,您可能会忘记在onCreate方法中创建DataBaseCreator。
baseCreator = new DataBaseCreator(this.getApplicationContext());
<强>更新强>
将列_id添加到您的表中:
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE Set_message ("+
"_id INTEGER PRIMARY KEY AUTOINCREMENT,"+
"Tittle VARCHAR(30) NOT NULL," +
"Message VARCHAR(30) NOT NULL,"+
"Date_from DATETIME NOT NULL,"+
"Start_Time DATETIME NOT NULL,"+
"Expiry_Date DATETIME NOT NULL,"+
"Expiry_Time DATETIME NOT NULL)");
}