0

我正在做一项活动来衡量一个人进行锻炼需要多长时间,但它有一个我还无法解决的错误......

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

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

在我的调试中,我可以看到变量“remainingSets”具有正确的值remainingSets = getIntent().getIntExtra("remaining_sets", 3)(这种情况:if (remainingSets > 0){...}

这是我的训练片段:

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

还有我的运动事件:

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

谁能给我任何帮助?

4

1 回答 1

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是内部类,因此它隐含地持有对其封闭的 . 的引用ExcerciseActivityExcerciseActivity这意味着您的代码将永远保留每个实例。不好。

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

在您的 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中,您取消注册您的侦听器,防止内存泄漏并防止对附加到不再存在的活动的 PopupExerciseListeners 进行奇怪的多次回调。

于 2013-02-28T20:13:19.500 回答