各アクティビティに独自のタイマーがある Android アプリに取り組んでいます。もともと私はすべてのクラスでこのようなコードを持っていました
public void tTimer() {
mTextField = (TextView) findViewById(R.id.countDown_timer);
final CountDownTimer tCounter = new CountDownTimer(7000, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTextField
.setText("Time Remaining: "
+ String.format(
"%d min, %d sec",
TimeUnit.MILLISECONDS
.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS
.toSeconds(millisUntilFinished)
- TimeUnit.MINUTES
.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millisUntilFinished))));
}
@Override
public void onFinish() {
mTextField.setText("Done!");
Intent i = new Intent (ColdActivity.this, PopUpActivity.class);
startActivity(i);
}
};
Button startButton = (Button) findViewById(R.id.timer_button_start);
/* When the Start Button is touched, the Countdown Timer will start */
startButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
ColdActivity.this);
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
tCounter.start();
// Once the user has been notified that they should increase
// their alarm volume, and they dismiss the notification, the timer
// will start
}
});
builder.setTitle("Notification");
builder.setMessage("It is recommended that you turn your Alarm/Ringtone volume on so that you can hear the alarm when it is finished");
AlertDialog dlg = builder.create();
dlg.show();
}
});
Button stopButton = (Button) findViewById(R.id.timer_button_cancel);
/* When the Stop Button is touched, the Countdown Timer will stop */
stopButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
tCounter.cancel();
}
});
}
tTimer()
上記のコードのように呼び出されるメソッドを持つ新しいクラスを作成しようとしています。これにより、そのクラスの新しいオブジェクトをインスタンス化しonCreate()
、そのメソッドを で呼び出すことができます。これにより、タイマー コードを書き直す必要がなくなります。以前と同じように、すべてのクラスで。
私はこのようにメソッドをパラメータ化しようとしました:
CustomTimerActivity cGT = new CustomTimerActivity();
cGT.tTimer(7000, ColdActivity.this, R.id.countDown_timer, R.id.timer_button_start, R.id.timer_button_cancel);
そして、私が取得し続けているのは、呼び出された私のクラスを指す NullPointerException です。CustomTimerActivity
リソース ID をパラメーターとして渡すことができないのはなぜですか? それとも、NPE は別のものから来ているのでしょうか?
これがCustomTimerActivity
私が作ったものです:
public class CustomTimerActivity extends Activity {
TextView mTextField;
int mId;
public void tTimer(long millisecs, final Activity activity1, int tv, int start, int cancel) {
mTextField = (TextView) findViewById(tv);
final CountDownTimer tCounter = new CountDownTimer(millisecs, 1000) {
@Override
public void onTick(long millisUntilFinished) {
mTextField
.setText("Time Remaining: "
+ String.format(
"%d min, %d sec",
TimeUnit.MILLISECONDS
.toMinutes(millisUntilFinished),
TimeUnit.MILLISECONDS
.toSeconds(millisUntilFinished)
- TimeUnit.MINUTES
.toSeconds(TimeUnit.MILLISECONDS
.toMinutes(millisUntilFinished))));
}
@Override
public void onFinish() {
mTextField.setText("Done!");
Intent i = new Intent (activity1, PopUpActivity.class);
startActivity(i);
}
};
Button startButton = (Button) findViewById(start);
/* When the Start Button is touched, the Countdown Timer will start */
startButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(
activity1);
builder.setNegativeButton("Dismiss",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
tCounter.start();
// Once the user has been notified that they should increase
// their alarm volume, and they dismiss the notification, the timer
// will start
}
});
builder.setTitle("Notification");
builder.setMessage("It is recommended that you turn your Alarm/Ringtone volume on so that you can hear the alarm when your tea is finished");
AlertDialog dlg = builder.create();
dlg.show();
}
});
Button stopButton = (Button) findViewById(cancel);
/* When the Stop Button is touched, the Countdown Timer will stop */
stopButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
tCounter.cancel();
}
});
}
}
findViewById()
問題は、入力として使用される Id と関係があるに違いありませんよね?
Intent が呼び出されたときに取得する別の NullPointerException を次に示します。