1

不知道为什么我会收到这个错误。

这是错误:

06-09 11:09:13.875: E/AndroidRuntime(11336): FATAL EXCEPTION: main
06-09 11:09:13.875: E/AndroidRuntime(11336): java.lang.IllegalStateException: Could not execute method of the activity
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.view.View$1.onClick(View.java:2144)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.view.View.performClick(View.java:2485)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.view.View$PerformClick.run(View.java:9080)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.os.Handler.handleCallback(Handler.java:587)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.os.Handler.dispatchMessage(Handler.java:92)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.os.Looper.loop(Looper.java:123)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.app.ActivityThread.main(ActivityThread.java:3683)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at java.lang.reflect.Method.invoke(Method.java:507)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at dalvik.system.NativeStart.main(Native Method)
06-09 11:09:13.875: E/AndroidRuntime(11336): Caused by: java.lang.reflect.InvocationTargetException
06-09 11:09:13.875: E/AndroidRuntime(11336):    at java.lang.reflect.Method.invokeNative(Native Method)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at java.lang.reflect.Method.invoke(Method.java:507)
06-09 11:09:13.875: E/AndroidRuntime(11336):    at android.view.View$1.onClick(View.java:2139)
06-09 11:09:13.875: E/AndroidRuntime(11336):    ... 11 more
06-09 11:09:13.875: E/AndroidRuntime(11336): Caused by: java.lang.NullPointerException
06-09 11:09:13.875: E/AndroidRuntime(11336):    at com.myapp.MainActivity.onClick(MainActivity.java:87)
06-09 11:09:13.875: E/AndroidRuntime(11336):    ... 14 more

这是代码:

public void onClick(View view) {        
    ListView l = getListView();
    int position = l.getPositionForView(view);

    cursor = ((SimpleCursorAdapter)l.getAdapter()).getCursor();
    cursor.moveToPosition(position);

    long id = cursor.getLong(cursor.getColumnIndex(MySQLiteHelper.COLUMN_ID));
    String name = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
    String day = cursor.getString(cursor.getColumnIndex(MySQLiteHelper.COLUMN_DAY));

    switch (view.getId()) {
    case R.id.button_day_edit:
        Intent startDayEdit = new Intent(this, DayEditActivity.class);
        startDayEdit.putExtra("dayID", id);
        startDayEdit.putExtra("dayName", name);
        startDayEdit.putExtra("dayDay", day);
        this.startActivity(startDayEdit);
        break;

    case R.id.button_day_delete:
        toast_deleted.setText(getString(R.string.toast_day_deleted));
        toast_deleted.show();
        datasource.deleteDay(id);
        cursor = datasource.fetchAllDays();
        dataAdapter.changeCursor(cursor);
        break;
    }
}

列表项中有 2 个图像视图,一个为该特定项目打开编辑活动,另一个将其删除。无论出于何种原因,我都会收到此调用目标异常,但我不知道为什么。

4

1 回答 1

0

InvocationTargetException只是动态调用中抛出的异常的包装器。真正的问题是它包装的NullPointerException

引起:java.lang.NullPointerException 06-09 11:09:13.875: E/AndroidRuntime(11336): at com.myapp.MainActivity.onClick(MainActivity.java:87) 06-09 11:09:13.875: E/ AndroidRuntime(11336): ... 14 更多

在 onclick 方法之后调试您的代码,您肯定会发现 listView 或位置存在空指针异常。

我希望这肯定会帮助你。谢谢

于 2013-06-18T03:38:33.213 回答