user1196969
user1196969

Reputation: 335

Sqlite Database:- Activity unable to start activity info

I am trying to fetch the values from the database but i am getting the Exception unable to instantiate the activity componentinfo what is the problem I have mentioned all the activity names in the manifest file but still it force close.

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;

  }

}

& my main activity where i am trying to fetch the vales is

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

Upvotes: 2

Views: 358

Answers (1)

PhatHV
PhatHV

Reputation: 8141

You may forget create DataBaseCreator in onCreate method before open.

baseCreator =  new DataBaseCreator(this.getApplicationContext());

Updated:

Add column _id to your table:

@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)");

}

Upvotes: 2

Related Questions