I’m trying to have a menu button to clear my database when it is pressed. That works well as far as I can tell. However, as soon as new information is sent to the database, the app force closes. How should I stop this?
Here is the clear part:
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
switch(item.getItemId()) {
case INSERT_ID:
//reset just makes all the variables in the thread reinitialize at base values
mLemonadeMainMenuView.reset();
NotesMade = 0;
mDbHelper.deleteAllNotes();
return true;
}
Here is deleteallnotes():
public boolean deleteAllNotes() { return mDb.delete(DATABASE_TABLE,"1", null) > 0; }
Here is the new data information:
@Override
public boolean onTouchEvent(MotionEvent event) {
mDbHelper.createNote(event.getX(), event.getY(), 1);
NotesMade ++;
mLemonadeMainMenuView.setCoords(NotesMade);
return true;
}
Finally, here is the receiving end:
while(mNotesMade>mNoteId)
{mNoteId++;
Cursor note = mDbHelper.fetchNote(mNoteId);
Float x;
Float y;
Float size;
x = Float.valueOf(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_X)));
y= Float.valueOf(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_Y)));
size =Float.valueOf(note.getString(note.getColumnIndexOrThrow(NotesDbAdapter.KEY_X)));
canvas.drawCircle(x, y, 1, paint);}
}
Note: I have already tried starting and stopping the thread in addition to the above.
Logcat:12-30 11:51:29.812: E/AndroidRuntime(6311): FATAL EXCEPTION: Thread-10
12-30 11:51:29.812: E/AndroidRuntime(6311): android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
12-30 11:51:29.812: E/AndroidRuntime(6311): at android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:214)
12-30 11:51:29.812: E/AndroidRuntime(6311): at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:41)
12-30 11:51:29.812: E/AndroidRuntime(6311): at com.drawing.LemonadeMainMenuView$LemonadeMainMenuThread.doDraw(LemonadeMainMenuView.java:75)
12-30 11:51:29.812: E/AndroidRuntime(6311): at com.drawing.LemonadeMainMenuView$LemonadeMainMenuThread.run(LemonadeMainMenuView.java:52)
Edit: solved – see Community Wiki answer.
In SQL, you arrange to execute:
or:
or:
On the face of it, the
mDb.delete()method drops the table rather than deleting the data from the table. There’s a reason why SQL uses CREATE and DROP separately from INSERT, DELETE and UPDATE.