2011-01-23 1 views
1
package com.android.countdown; 
import android.app.Activity; 
import android.os.Bundle; 
import android.os.CountDownTimer; 
import android.widget.TextView; 

public class CountDownTest extends Activity { 

TextView tv; //textview to display the countdown 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 

TextView tv = new TextView(this); 
this.setContentView(tv); 

//5000 is the starting number (in milliseconds) 
//1000 is the number to count down each time (in milliseconds) 
MyCount counter = new MyCount(5000,1000); 

counter.start(); 

} 

//countdowntimer is an abstract class, so extend it and fill in methods 
public class MyCount extends CountDownTimer{ 

public MyCount(long millisInFuture, long countDownInterval) { 
super(millisInFuture, countDownInterval); 
} 

@Override 
public void onFinish() { 
tv.setText(”done!”); 
} 

@Override 
public void onTick(long millisUntilFinished) { 
tv.setText(”Left: ” + millisUntilFinished/1000); 

} 
+0

たラインについて、それが言うん:

エンドMyCountクラスは、このように見えるでしょうか? –

答えて

1

あなたは作成中に「tv」を再宣言しているので、新しいtextViewとして設定していません。

TextView tv = new TextView(this); 

tv = new TextView(this); 

への編集: もう一つの問題:あなたは別のカウンタークラスを肝炎、この行を変更します。そのクラスはcountDownTestアクティビティのプロパティにアクセスできません。だから、 "tv"変数はそこでは空です。ランダムなサブクラスにスーパークラスの変数を使用させることはできません。 私はあなたのクラスのデザインに戻り、何がどこに行くのか理解しておくべきだと思いますか?

は(setId()と私は思う)あなたのTextView IDを与える:あなただけテストしている場合はその間に

は、あなたはこのような何かを行うことができます。 mycountクラスに findViewById()を使用してテキストビューを取得してください。 これを使用してテキストを変更します。

または

パラメータ「のTextView」を持っているあなたのMyCountにmemeberを追加し、あなたのorigionalテレビVARとそれを呼び出します。

+0

ああ、申し訳ありませんが、私は元々それを持っていた方法だと言及することを忘れてしまった。それでも同じエラーがあります – MJ93

0

nanneはこの行の前にタイプを削除するだけです。tv = new TextView(this);

あなたのクラスがこのアクティビティの内部クラスであり、tvがその中のすべてのメソッドと内部クラスを含むすべてのアクティビティのvaribaleとして定義されているため、これが唯一必要な変更です。

このコードを変更すると、テストしました。

しかし、あなたのMyCountファイルが別の.javaファイル内にある場合、あなたはこのように、このビュー合格する必要があります。

MyCountカウンタ=新しいMyCount(5000,1000、テレビを)。

public class MyCount extends CountDownTimer{ 
TextView tv; 

public MyCount(long millisInFuture, long countDownInterval, TextView tvx) { 
super(millisInFuture, countDownInterval); 
tv = tvx; 
} 

@Override 
public void onFinish() { 
tv.setText("done!"); 
} 

@Override 
public void onTick(long millisUntilFinished) { 
tv.setText("Left: " + millisUntilFinished/1000); 

}} 
関連する問題