变量的值突然设置为0

时间:2013-02-27 21:31:29

标签: android android-activity

我正在做一项活动,以衡量一个人做运动需要多长时间,但它有一个我无法解决的错误......

TrainingFragment显示一个练习列表,用户可以单击该练习,然后启动我的ExerciseActivity并运行,直到变量“remainingsSets”设置为0。

当我在任何练习中第一次点击时,一切正常,ExerciseActivity正常工作结束返回TrainingFragment。但是,如果我尝试点击另一个练习,则练习活动就会关闭。

在我的调试中,我可以看到变量“remainingSets”带有正确的值(remainingSets = getIntent().getIntExtra("remaining_sets", 3)),但是当单击startButton时,我不知道为什么变量“remainingSets”被设置为0然后关闭活动,因为这个条件:if (remainingSets > 0){...}

这是我的TrainingFragment:

public class TrainingFragment extends Fragment {

private final static int START_EXERCISE = 1;

private Training training;
private String lastItemClicked;
private String[] values;

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    Bundle bundle = getArguments();
    if (bundle != null) {
        training = bundle.getParcelable("training");
    }
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    return (ScrollView) inflater.inflate(R.layout.template_exercises, container, false);
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    LinearLayout exercisesContainer = (LinearLayout) getView().findViewById(R.id.exercises);
    LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    List<Exercise> exercises = training.getExercises();
    values = new String[exercises.size()];

    if (savedInstanceState != null) {
        values = savedInstanceState.getStringArray("values");
    }

    for (int i = 0; i < exercises.size(); i++) {
        final View exerciseView = inflater.inflate(R.layout.template_exercise, null);

        exerciseView.setTag(String.valueOf(i));

        TextView remainingSets = (TextView) exerciseView.findViewById(R.id.remaining_sets);

        if (savedInstanceState != null) {
            remainingSets.setText(values[i]);
        } else {
            String sets = exercises.get(i).getSets();
            remainingSets.setText(sets);
            values[i] = sets;
        }

        exerciseView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(getActivity(), ExerciseActivity.class);
                intent.putExtra("remaining_sets",
                        Integer.valueOf(((TextView) v.findViewById(R.id.remaining_sets)).getText().toString()));

                lastItemClicked = v.getTag().toString();

                startActivityForResult(intent, START_EXERCISE);
            }
        });

        exercisesContainer.addView(exerciseView);
    }
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
    outState.putStringArray("values", values);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    View view = ((LinearLayout) getView().findViewById(R.id.exercises)).findViewWithTag(lastItemClicked);

    if (requestCode == START_EXERCISE) {
        if (resultCode == Activity.RESULT_OK) { // the exercise had been
                                                // finished.
            ((TextView) view.findViewById(R.id.remaining_sets)).setText("0");

            view.setClickable(false);

            values[Integer.valueOf(lastItemClicked)] = "0";

        } else if (resultCode == Activity.RESULT_CANCELED) {

            String remainingSets = data.getStringExtra("remaining_sets");
            ((TextView) view.findViewById(R.id.remaining_sets)).setText(remainingSets);

            values[Integer.valueOf(lastItemClicked)] = remainingSets;
        }
    }
}
}

我的运动活动:

public class ExerciseActivity extends Activity {

private Chronometer chronometer;
private TextView timer;
private Button startButton;
private Button endButton;
private int remainingSets;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exercise);

    ExerciseEvents.addExerciseListener(new PopupExerciseListener());

    chronometer = (Chronometer) findViewById(R.id.exercise_doing_timer);
    timer = (TextView) findViewById(R.id.timer);

    startButton = (Button) findViewById(R.id.start_exercise);
    startButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ExerciseEvents.onExerciseBegin();
        }
    });

    endButton = (Button) findViewById(R.id.end_exercise);
    endButton.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            ExerciseEvents.onExerciseRest();
        }
    });
}

@Override
public void onBackPressed() {
    Intent intent = new Intent();
    intent.putExtra("remaining_sets", String.valueOf(remainingSets));
    setResult(RESULT_CANCELED, intent);

    super.onBackPressed();
}

public class PopupExerciseListener implements ExerciseListener {

    public PopupExerciseListener() {
        remainingSets = getIntent().getIntExtra("remaining_sets", 3);
    }

    @Override
    public void onExerciseBegin() {
        if (remainingSets > 0) {
            chronometer.setVisibility(View.VISIBLE);
            timer.setVisibility(View.GONE);

            chronometer.setBase(SystemClock.elapsedRealtime());
            chronometer.start();

            startButton.setVisibility(View.GONE);
            endButton.setVisibility(View.VISIBLE);
        } else {
            ExerciseEvents.onExerciseFinish();
        }
    }

    @Override
    public void onExerciseFinish() {
        setResult(RESULT_OK);

        finish();
    }

    @Override
    public void onExerciseRest() {
        chronometer.setVisibility(View.GONE);
        endButton.setVisibility(View.GONE);
        timer.setVisibility(View.VISIBLE);

        long restTime = getIntent().getLongExtra("time_to_rest", 60) * 1000;
        new CountDownTimer(restTime, 1000) {

            @Override
            public void onTick(long millisUntilFinished) {
                timer.setText(String.valueOf(millisUntilFinished / 1000));
            }

            @Override
            public void onFinish() {
                ExerciseEvents.onExerciseBegin();
            }
        }.start();

        remainingSets--;
    }
}
}

我的ExerciseEvents:

public class ExerciseEvents {

private static LinkedList<ExerciseListener> mExerciseListeners = new LinkedList<ExerciseListener>();

public static void addExerciseListener(ExerciseListener listener) {
    mExerciseListeners.add(listener);
}

public static void removeExerciseListener(String listener) {
    mExerciseListeners.remove(listener);
}

public static void onExerciseBegin() {
    for (ExerciseListener l : mExerciseListeners) {
        l.onExerciseBegin();
    }
}

public static void onExerciseRest() {
    for (ExerciseListener l : mExerciseListeners) {
        l.onExerciseRest();
    }
}

public static void onExerciseFinish() {
    for (ExerciseListener l : mExerciseListeners) {
        l.onExerciseFinish();
    }
}

public static interface ExerciseListener {

    public void onExerciseBegin();

    public void onExerciseRest();

    public void onExerciseFinish();
}
}

有人可以给我任何帮助吗?

1 个答案:

答案 0 :(得分:1)

更新代码后,我发现您的代码中存在大量内存泄漏:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_exercise);

    ExerciseEvents.addExerciseListener(new PopupExerciseListener());
    ....
}

致电ExerciseEvents.addExerciseListener(new PopupExerciseListener())将新PopupExerciseListener添加到静态/全局列表:ExcerciseEvents.mExerciseListeners。由于类PopupExerciseListener是一个内部类,它隐含地包含对其封闭ExcerciseActivity的引用。这意味着您的代码永远保留在ExcerciseActivity的每个实例上。不好。

这也可以解释你看到的奇怪行为。当调用其中一个onExcersizeXXX()方法时,它将调用链表中的所有ExcerciseListener,即前一个屏幕和当前屏幕中的ExcerciseListener。

在ExcerciseActivity.java中尝试此操作:

....
ExerciseListener mExerciseListener;
....

@Override
protected void onCreate(Bundle savedInstanceState) {
    ....
    ....
    mExerciseListener = new PopupExerciseListener()
    ExerciseEvents.addExerciseListener(mExerciseListener);
    ....
    ....
}

@Override
protected void onDestroy() {
    ExerciseEvents.removeExerciseListener(mExerciseListener);
    super.onDestroy();
}

....

onDestroy中,您取消注册侦听器,防止内存泄漏并防止附加到不再存在的活动的PopupExerciseListener的奇怪多次回调。