String cannot be cast to long
Ошибка
java.lang.ClassCastExseption: java.lang.String cannot be cast to java.lang.Long
long duration = TimeUnit.HOURS.toMillis(24);
long endOfTask = System.currentTimeMillis() + duration;
long finalTime = save.getLong(APP_PREFERENCES_CHRONOMETER, endOfTask);
long finalLong = finalTime - System.currentTimeMillis();
CountDownTimer timer = new CountDownTimer( finalLong, 1000) {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onTick(long l) {
String duration = String.format(Locale.ENGLISH, "%02d : %02d : %02d", TimeUnit.MILLISECONDS.toHours(l), TimeUnit.MILLISECONDS.toMinutes(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toHours(l)), (l / 1000) % 60);
chronometer.setText(duration);
}
@Override
public void onFinish() {
try {
Intent intent = new Intent(StartGame.this, EndGame.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
}
}; timer.start();
Ругается на эту строку
CountDownTimer timer = new CountDownTimer( finalLong, 1000) {
Вот хоть убейте, при чём тут String я не понимаю, там же везде только long. Помогите советом, пожалуйста.
P.S.: До этого было так:
CountDownTimer timer = new CountDownTimer( duration, 1000) {
и всё работало. Но это не то, что мне нужно.
!
Строка 144 это та, что я указал выше.
Весь код, удалил только кнопки:
public class StartGame extends AppCompatActivity {
long duration = TimeUnit.HOURS.toMillis(24);
long endOfTask = System.currentTimeMillis() + duration;
final String TAG = "STATE";
private long backPressedTime;
public static Toast backToast;
static SharedPreferences save;
public static final String APP_PREFERENCES = "";
public static final String APP_PREFERENCES_TODO = "";
public static final String APP_PREFERENCES_CHRONOMETER = "";
Context context;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.start_game);
context = this;
saveText();
loadText();
//Массив
String[] tasks = {"..."};
// Вычисляем, сколько слов в массиве
int tasksLength = tasks.length;
//Генерируем случайные числа
int rand = (int) (Math.random() * tasksLength);
//Ставим задачу
String task = tasks[rand] + " ";
TextView todo = (TextView) findViewById(R.id.todo);
todo.setText(task);
Window w = getWindow();
w.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Button buttonBack = findViewById(R.id.buttonBack);
buttonBack.setOnClickListener(v -> {
try {
Intent intent = new Intent(StartGame.this, MainActivity.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
});
void saveText () {
TextView todo = (TextView) findViewById(R.id.todo);
TextView chronometer = (TextView) findViewById(R.id.chronometer);
SharedPreferences save = StartGame.this.getSharedPreferences("save", MODE_PRIVATE);
SharedPreferences.Editor editor = save.edit();
editor.putLong(APP_PREFERENCES_CHRONOMETER, endOfTask);
editor.putString(APP_PREFERENCES_TODO, todo.getText().toString());
editor.apply();
}
void loadText () {
TextView todo = (TextView) findViewById(R.id.todo);
TextView chronometer = (TextView) findViewById(R.id.chronometer);
SharedPreferences save = StartGame.this.getSharedPreferences("save", MODE_PRIVATE);
todo.setText(save.getString(APP_PREFERENCES_TODO, todo.getText().toString()));
long finalTime = save.getLong(APP_PREFERENCES_CHRONOMETER, endOfTask);
long finalLong = finalTime - System.currentTimeMillis();
CountDownTimer timer = new CountDownTimer( finalLong, 1000) {
@RequiresApi(api = Build.VERSION_CODES.N)
@Override
public void onTick(long l) {
String duration = String.format(Locale.ENGLISH, "%02d : %02d : %02d", TimeUnit.MILLISECONDS.toHours(l), TimeUnit.MILLISECONDS.toMinutes(l) - TimeUnit.MINUTES.toSeconds(TimeUnit.MILLISECONDS.toHours(l)), (l / 1000) % 60);
chronometer.setText(duration);
}
@Override
public void onFinish() {
try {
Intent intent = new Intent(StartGame.this, EndGame.class);
startActivity(intent);
finish();
} catch (Exception e) {
}
}
}; timer.start();
}
@Override
protected void onPause() {
super.onPause();
saveText();
Log.d(TAG, "onPause");
}
@Override
protected void onStop() {
saveText();
super.onStop();
Log.d(TAG, "onDestroy");
}
@Override
protected void onResume() {
super.onResume();
loadText();
Log.d(TAG, "onResume");
}
@Override
protected void onDestroy() {
super.onDestroy();
saveText();
Log.d(TAG, "onDestroy");
}