몇 분과 초를 선택하는 데 사용되는 두 개의 NumberPicker로 간단한 앱을 만들고 있습니다. 내가 여기에 게시 할 수 있도록 너무 많은 코드가되지 않습니다 :앱이 시작되기 전에 충돌이 발생합니다.
public class MainActivity extends Activity implements OnClickListener {
private static final String TAG = "Interval Trainer";
private CountDownTimer countDownTimer;
private boolean timerHasStarted = false;
private Button startButton;
public TextView text;
private final long interval = 1 * 1000;
//Create NumberPickers
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1);
NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2);
@Override
protected void onCreate(Bundle savedInstanceState) {
Log.i(TAG,"Entering onCreate()");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) this.findViewById(R.id.button);
startButton.setOnClickListener(this);
text = (TextView) this.findViewById(R.id.timer);
//Set min and max values for NumberPickers
numberPicker1.setMaxValue(100);
numberPicker1.setMinValue(0);
numberPicker2.setMaxValue(59); //This is the seconds picker
numberPicker2.setMinValue(0);
Log.i(TAG,"Exiting onCreate()");
}
@Override
public void onClick(View v) {
//Calculate total time from NumberPickers in seconds
long startTime = (numberPicker1.getValue() * 60) + numberPicker2.getValue();
//Create CountDownTimer with values from NumberPickers
countDownTimer = new MyCountDownTimer(startTime, interval);
text.setText(text.getText() + String.valueOf(startTime/1000)); //should be removed
if(!timerHasStarted) {
countDownTimer.start();
timerHasStarted = true;
startButton.setText("STOP");
} else {
countDownTimer.cancel();
timerHasStarted = false;
startButton.setText("RESTART");
}
//Disable the NumberPickers after 'Start' is pressed
numberPicker1.setEnabled(false);
numberPicker2.setEnabled(false);
}
public class MyCountDownTimer extends CountDownTimer {
public MyCountDownTimer(long startTime, long interval) {
super(startTime, interval);
}
@Override
public void onFinish() {
text.setText("Time's up!");
//re-enable the NumberPickers once countdown is done
numberPicker1.setEnabled(true);
numberPicker2.setEnabled(true);
}
@Override
public void onTick(long millisUntilFinished) {
text.setText("" + millisUntilFinished/1000);
//Changes the value of the NumberPickers after each tick
}
}
}
는 내가 가지고 (이 자동으로 정적 만들 것?) 충돌이 날은 어떤 방법 이외의 두 NumberPickers를 선언 관련이 느낌. 원래 onCreate()에는이 두 줄의 코드가 있었지만 내부 클래스와 다른 메서드에서 필요하기 때문에 바깥으로 옮겼습니다. 이것이 충돌 사고의 원인입니까? 그렇다면 올바르게 수행하고 내 수업 (inner class 포함) 주변의 numberPicker1 및 2에 액세스 할 수있는 방법은 무엇입니까?
감사합니다.
또한 예를 들어
후에서 onCreate에 반대 initlize 수 있습니다 – Fllo