I've been trying to make a timer app. The user will input seconds and the seek bar moves in a decreasing manner. But as soon as I click on the button, the progress bar moves to the value, but the app crashes. I think the code is working fine till the progress bar update, but after that, it crashes. I have been struggling with this code for hours. Please help, I am a newbie in android app development.
Here is the log:-
2020-02-11 13:17:21.081 4121-4121/com.example.timer E/m.example.time: Invalid ID 0x00000004.
2020-02-11 13:17:21.081 4121-4121/com.example.timer D/AndroidRuntime: Shutting down VM
2020-02-11 13:17:21.083 4121-4121/com.example.timer E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.timer, PID: 4121
android.content.res.Resources$NotFoundException: String resource ID #0x4
at android.content.res.Resources.getText(Resources.java:348)
at android.widget.TextView.setText(TextView.java:5846)
at com.example.timer.MainActivity$1.onTick(MainActivity.java:29)
at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:130)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6715)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
XML
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="372dp"
android:layout_height="41dp"
android:layout_marginStart="17dp"
android:layout_marginLeft="17dp"
android:layout_marginTop="15dp"
android:layout_marginEnd="22dp"
android:layout_marginRight="22dp"
android:gravity="center_horizontal"
android:text="Enter Time In Seconds"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="99dp"
android:layout_marginLeft="99dp"
android:layout_marginTop="26dp"
android:layout_marginEnd="99dp"
android:layout_marginRight="99dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<SeekBar
android:id="@+id/seekBar"
android:layout_width="372dp"
android:layout_height="65dp"
android:layout_marginStart="23dp"
android:layout_marginLeft="23dp"
android:layout_marginTop="163dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="248dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="163dp"
android:layout_marginLeft="163dp"
android:layout_marginTop="134dp"
android:layout_marginEnd="160dp"
android:layout_marginRight="160dp"
android:layout_marginBottom="66dp"
android:onClick="start"
android:text="Start"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/seekBar" />
<TextView
android:id="@+id/textView2"
android:layout_width="235dp"
android:layout_height="37dp"
android:layout_marginStart="88dp"
android:layout_marginLeft="88dp"
android:layout_marginTop="113dp"
android:layout_marginEnd="88dp"
android:layout_marginRight="88dp"
android:layout_marginBottom="13dp"
android:gravity="center_horizontal"
android:text="000"
android:textSize="30sp"
app:layout_constraintBottom_toTopOf="@+id/seekBar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editText" />
</androidx.constraintlayout.widget.ConstraintLayout>
JAVA
package com.example.timer;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void start(View view){
EditText editText = findViewById(R.id.editText);
final TextView textView = findViewById(R.id.textView2);
final SeekBar seekBar = findViewById(R.id.seekBar);
int timeGiven = Integer.parseInt(editText.getText().toString());
seekBar.setMax(timeGiven);
seekBar.setProgress(timeGiven);
new CountDownTimer(timeGiven*1000, 1000){
public void onTick(long millisecondsLeft){
int secondsLeft = (int) millisecondsLeft/1000;
seekBar.setProgress(secondsLeft);
textView.setText(secondsLeft);
}
public void onFinish(){
Toast.makeText(getApplicationContext(),"BOOM",Toast.LENGTH_SHORT).show();
}
}.start();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
I think problem is here:
textView.setText(secondsLeft);
try:
textView.setText(String.valueOf(secondsLeft));
It's not possible to place integer values into TextView.
User contributions licensed under CC BY-SA 3.0