2012-03-18 4 views
0

다음은 카운트 다운 타이머를 사용하는 프로그램이지만 프로그램이 에뮬레이터에서 실행될 때 "죄송합니다. AndroidTestTimer (프로세스 android.test.timer) 프로세스가 예기치 않게 중지되었습니다. 다시 시도하십시오. 강제 종료 버튼이있는.안드로이드에서 CountDownTimer를 사용하는 방법?

다음은 코드입니다.

package android.test.timer; 

import android.os.CountDownTimer; 
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.TextView; 

public class AndroidTestTimerActivity extends Activity { 
    /** Called when the activity is first created. */ 
    TextView tv; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     TextView tv = (TextView)findViewById(R.id.time_textview); 
     tv.setText("Default!"); 
     MyTimer tim = new MyTimer(6000,1000); 
     tim.start(); 
    } 

    public class MyTimer extends CountDownTimer { 
     public MyTimer(long millisInFuture, long countDownInterval) { 
      super(millisInFuture, countDownInterval); 
      // TODO Auto-generated constructor stub 
      tv.setText("changed by the constructor"); 
     } 

     @Override 
     public void onFinish() { 
      // TODO Auto-generated method stub 
      tv.setText("changed by the onFinish"); 
     } 

     @Override 
     public void onTick(long millisUntilFinished) { 
      // TODO Auto-generated method stub 
      tv.setText("time: " + millisUntilFinished); 
     } 

    } 
} 
+0

은이 줄을 사용합니다. tv = (TextView) findViewById (R.id.time_textview); TextView 대신 tv = (TextView) findViewById (R.id.time_textview); –

답변

3

당신은 초기화되지 않습니다 활동에 너무, onCreate()tvtv 변수를 재 선언.
오른쪽 코드 :

tv = (TextView) findViewById(R.id.time_textview); 

P.S. 다음 번에 logcat 출력을 추가하십시오. "죄송합니다 응용 프로그램이 예기치 않게 중지되었습니다"오류에 대해서는 아무 것도 말하지 않습니다.

oncreate 부분의
+0

오 감사합니다! 나는 그것을 고치려고 몇 시간을 보냈다! –

관련 문제