对listview行内的视图进行invocationtargetexception

时间:2013-06-18 03:28:34

标签: android nullpointerexception invocationtargetexception

不确定我为什么会收到此错误。

这是错误:

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

theres 2在列表项内部的图像视图,一个用于显示该特定项的编辑活动,另一个删除它。无论出于何种原因,我得到这个invocationtargetexception,我不知道为什么。

1 个答案:

答案 0 :(得分: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或位置的空指针异常。

我希望这肯定能帮到你。 感谢