似乎无法将 Chronometer 转换为 int
Cant seem to cast the Chronometer to an int
我正在尝试将计时器转换为秒,但似乎无法将其转换为 int。
因为我需要将秒数放在新的 activity.
上
不可转换的类型;无法将 'android.widget.Chronometer' 转换为 'int'
private Chronometer chronometer;
private boolean ChronometerRunnin;
private long pauseOffset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ButterKnife.bind(this);
chronometer = findViewById(R.id.chronometerT);
chronometer.setFormat("Timer: %s");
buttonStats = findViewById(R.id.btRun);
buttonStats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openStatsPage();
}
});
}
public void openStatsPage(){
Intent intent = new Intent(this, StatsPage.class);
int elapsedMillis = (int) chronometer /1000;
float timerdata = Integer.valueOf(chronometer.getText().toString());
intent.putExtra("timedata",timerdata);
startActivity(intent);
}
您不能将 Chronometer 类型转换为 int,您需要调用 getBase()
方法。
尝试像这样更改您的 openStatsPage 方法
int elapsedTimeInSec = (int) ((SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000);
Intent intent = new Intent(this, StatsPage.class);
intent.putExtra("timedata", elapsedTimeInSec);
startActivity(intent);
我正在尝试将计时器转换为秒,但似乎无法将其转换为 int。 因为我需要将秒数放在新的 activity.
上不可转换的类型;无法将 'android.widget.Chronometer' 转换为 'int'
private Chronometer chronometer;
private boolean ChronometerRunnin;
private long pauseOffset;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ButterKnife.bind(this);
chronometer = findViewById(R.id.chronometerT);
chronometer.setFormat("Timer: %s");
buttonStats = findViewById(R.id.btRun);
buttonStats.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
openStatsPage();
}
});
}
public void openStatsPage(){
Intent intent = new Intent(this, StatsPage.class);
int elapsedMillis = (int) chronometer /1000;
float timerdata = Integer.valueOf(chronometer.getText().toString());
intent.putExtra("timedata",timerdata);
startActivity(intent);
}
您不能将 Chronometer 类型转换为 int,您需要调用 getBase()
方法。
尝试像这样更改您的 openStatsPage 方法
int elapsedTimeInSec = (int) ((SystemClock.elapsedRealtime() - chronometer.getBase()) / 1000);
Intent intent = new Intent(this, StatsPage.class);
intent.putExtra("timedata", elapsedTimeInSec);
startActivity(intent);