2012-03-18 6 views
0

以下はカウントダウンタイマーを使用するプログラムですが、エミュレータでプログラムを起動すると、というエラーメッセージが表示されます "申し訳ありませんAndroidTestTimer(プロセスandroid.test.timer)プロセスが予期せず停止しました。に強制終了ボタンがあります。AndDroidの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

oncreate部分では、次の行を使用します。tv =(TextView)findViewById(R.id.time_textview);テキストビューの代わりにtv =(TextView)findViewById(R.id.time_textview); –

答えて

3

あなたはonCreate()tv変数を再宣言、その活動のtvが初期化されていません。
右コード:

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

P.S.次回に問題の出力logcatを追加してください。 「申し訳ありませんが、アプリケーションが予期せず停止しました」というエラーは何も表示されません。

+0

ありがとう!私はそれを修正しようと時間を費やした! –

関連する問題