2014-04-02 2 views
0

몇 분과 초를 선택하는 데 사용되는 두 개의 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에 액세스 할 수있는 방법은 무엇입니까?

감사합니다.

답변

4

당신은 당신이 아직 초기화 객체 해달라고 그래서 단지 당신이 부하를 레이아웃에서 onCreate 후이

//Create NumberPickers 
NumberPicker numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1); 
NumberPicker numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2); 

같은 객체 절차를 시작할 기운 다. 당신은 당신이주의 깊게 코드를 읽을 경우, 당신은 텍스트 뷰의 텍스트 '는 이미 :) 않는'것을 볼 수,

// init variable 
NumberPicker numberPicker1 = null; 
NumberPicker numberPicker2 = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 

    setContentView(R.layout.activity_main); 
// After here only your Activity gets the layout objects 

    numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1); 
    numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2); 

} 
2

당신은 당신의 findViewByIdonCreate 내부 방법을 넣어 지역 변수 만들어야합니다

// init variable 
NumberPicker numberPicker1, numberPicker2; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    // ... 
    numberPicker1 = (NumberPicker) findViewById(R.id.numberPicker1); 
    numberPicker2 = (NumberPicker) findViewById(R.id.numberPicker2); 

} 

그런 다음, 당신이 할 다른 방법 내부 이러한 변수를 호출 할 것이다. this reference에 따르면

비 정적 중첩 클래스 (InnerClasses 것은)가 개인 선언 된 경우에도, 바깥 쪽 클래스의 다른 멤버에 액세스 할 수 있습니다. 정적 중첩 클래스는 해당 클래스의 다른 멤버에 액세스 할 수 없습니다. [...] InnerClass은 둘러싼 인스턴스의 메서드 및 필드에 직접 액세스 할 수 있습니다.

+0

또한 예를 들어

setContentView(R.layout.activity_main); 

후에서 onCreate에 반대 initlize 수 있습니다 – Fllo

관련 문제