在我的应用程序中,我有一个扩展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);
        }


    }

最佳答案

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

避免NullPointerException的另一种方法是

"Workout A".equals(acitivty)

10-07 15:43