0

在我的应用程序中,我有一个扩展 ListActivity 并使用 ListView 的类。我的 onListItemClick 方法就是不想工作。在方法内部,它必须确定被点击的项目是锻炼 A 还是锻炼 B。下面是代码:

@Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

        Long rowId = id;

        Cursor cursor = mDbHelper.fetchNote(rowId);

        String activity = cursor.getString(cursor.getColumnIndexOrThrow(StrongDbAdapter.WORKOUT_STATE));

        if(activity == "Workout A"){
            workoutAA = true;
        }else if(activity == "Workout B"){
            workoutAA = false;
        }


        if(workoutAA == true){
            Intent i = new Intent(this, WorkoutEditA.class);
            i.putExtra(StrongDbAdapter.KEY_ROWID, id);
            startActivityForResult(i, ACTIVITY_EDIT);
        }else if(activity == "Workout B"){
            Intent i = new Intent(this, WorkoutEditB.class);
            i.putExtra(StrongDbAdapter.KEY_ROWID, id);
            startActivityForResult(i, ACTIVITY_EDIT);
        }


    }
4

1 回答 1

2

activity.equals( "Workout A"). 当字符串比较使用equals()

避免 NullPointerException 的另一种方法是

"Workout A".equals(acitivty)
于 2012-08-08T19:55:38.387 回答