I’m building a task list app. For each task, a RelativeLayout is created, within which is a CheckBox and a TextView with the name of the task. Then it’s added to a pre-existing LinearLayout. That part all works.
I added a ‘Delete Task’ ImageButton that calls the method deleteTask(View view). deleteTask() which deletes the task from the database. When I try to run I get “java.lang.IllegalStateException: Could not execute method of the activity” along with a bunch of other errors.
01-16 13:15:48.975: E/AndroidRuntime(16881): FATAL EXCEPTION: main
01-16 13:15:48.975: E/AndroidRuntime(16881): java.lang.IllegalStateException: Could not execute method of the activity
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.view.View$1.onClick(View.java:3591)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.view.View.performClick(View.java:4084)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.view.View$PerformClick.run(View.java:16966)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.os.Handler.handleCallback(Handler.java:615)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.os.Handler.dispatchMessage(Handler.java:92)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.os.Looper.loop(Looper.java:137)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.app.ActivityThread.main(ActivityThread.java:4931)
01-16 13:15:48.975: E/AndroidRuntime(16881): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 13:15:48.975: E/AndroidRuntime(16881): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 13:15:48.975: E/AndroidRuntime(16881): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
01-16 13:15:48.975: E/AndroidRuntime(16881): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
01-16 13:15:48.975: E/AndroidRuntime(16881): at dalvik.system.NativeStart.main(Native Method)
01-16 13:15:48.975: E/AndroidRuntime(16881): Caused by: java.lang.reflect.InvocationTargetException
01-16 13:15:48.975: E/AndroidRuntime(16881): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 13:15:48.975: E/AndroidRuntime(16881): at java.lang.reflect.Method.invoke(Method.java:511)
01-16 13:15:48.975: E/AndroidRuntime(16881): at android.view.View$1.onClick(View.java:3586)
01-16 13:15:48.975: E/AndroidRuntime(16881): ... 11 more
01-16 13:15:48.975: E/AndroidRuntime(16881): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to android.widget.CheckBox
01-16 13:15:48.975: E/AndroidRuntime(16881): at com.rook.todolist.MainActivity.deleteTask(MainActivity.java:177)
01-16 13:15:48.975: E/AndroidRuntime(16881): ... 14 more
Here’s my code. Tried to include what I thought is relevant, obviously can include more if need be.
deleteTask() called by ImageButton :
public void deleteTask (View view) {
// instantiate dbhandler
DatabaseHandler db = new DatabaseHandler(this);
// get tasks
List<Task> tasks = db.getAllTasks();
for(Task t : tasks) {
CheckBox cb = (CheckBox) findViewById(t.getID() + 10000);
if(cb.isChecked()) { db.deleteTask(t); }
}
buildTaskList();
}//end deleteTask()
When I create the CheckBoxes, they needed to have an id that I could reference for LayoutParams, so I set their id to be 10,000 + the id of the task. Just something that’s easily rememberable.
buildTaskList() just removes all views from the master LinearLayout and rebuilds them based on tasks in the database.
deleteTask(Task task) called if CheckBox is checked.
// Deleting single task
public void deleteTask (Task task) {
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_TASKS, KEY_ID + " =?", new String[] { String.valueOf(task.getID()) });
}
The delete operation is still performed. When I reload the app, the task that was to be deleted is gone. Also, I tested deleteTask() when I first created it and it worked.
Edit
As many pointed out, the line findViewById(t.getID() + 10000); was returning an ImageView not a checkbox. But I put in log statements to spit out the ids both in the method where they are created and in the deleteTask() method and the ids are the same! Do the ids assigned to views change or something?
as in log :
means you are trying to cast ImageView to Checkbox . change your code as :