-2

I'm new on Android Studio and Java. I want to get a value from an EditText and convert it to an Integer and set it to itself in onClickListener method, but it keeps stopped responding after I pressed buttons(buttons that are worked for decreasing and/or increasing a value on an EditText, as stated with btnInc and btnDec) when it debugged on my phone. I debugged on an emulator and it didn't worked either. I declared a final EditText and final Button in the protected void onCreate method after everything is created.

Here's my code:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Button btnCalculate = findViewById(R.id.btn_calc);
    final EditText edtPrime = findViewById(R.id.EditText_numMaxPrime);
    final Button btnInc = findViewById(R.id.btn_incNum);
    final Button btnDec = findViewById(R.id.btn_decNum);

    btnDec.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Integer number = Integer.parseInt(edtPrime.getText().toString());
            number--;
            edtPrime.setText(number, TextView.BufferType.EDITABLE);
        }
    });
    btnInc.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Integer number = Integer.parseInt(edtPrime.getText().toString());
            number++;
            edtPrime.setText(number, TextView.BufferType.EDITABLE);
        }
    });
    btnCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //Sieve(Integer.parseInt(edtPrime.getText().toString()));
        }
    });
}

I had imported android.widget.Button and android.widget.EditText.

Here's my XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="karuntodroid.sieveoferatosthenes.MainActivity">

    <TextView
        android:id="@+id/TextView_askPrime"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginTop="8dp"
        android:text="Enter the prime number up to:"
        app:layout_constraintBottom_toBottomOf="@+id/EditText_numMaxPrime"
        app:layout_constraintHorizontal_bias="0.051"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="@+id/EditText_numMaxPrime"
        app:layout_constraintVertical_bias="0.032" />

    <EditText
        android:id="@+id/EditText_numMaxPrime"
        android:layout_width="60dp"
        android:layout_height="40dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:ems="10"
        android:hint="100"
        android:inputType="number"
        app:layout_constraintEnd_toStartOf="@+id/btn_incNum"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_incNum"
        android:layout_width="40dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="+"
        app:layout_constraintEnd_toStartOf="@+id/btn_decNum"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_decNum"
        android:layout_width="40dp"
        android:layout_height="0dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="-"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btn_calc"
        android:layout_width="wrap_content"
        android:layout_height="0dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="CALCULATE"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/TextView_askPrime" />

    <android.support.constraint.ConstraintLayout
        android:layout_width="0dp"
        android:layout_height="407dp"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent">

    <TextView
        android:id="@+id/TextView_numbersare"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="8dp"
        android:text="Numbers are:"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/TextView_numbers"
        android:layout_width="350dp"
        android:layout_height="309dp"
        android:layout_marginEnd="8dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:text="numbers"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/TextView_numbersare" />

    <Button
        android:id="@+id/btn_numbercopy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="Copy"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toStartOf="@+id/btn_numsReset"
        app:layout_constraintTop_toBottomOf="@+id/TextView_numbers" />

    <Button
        android:id="@+id/btn_numsReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginTop="8dp"
        android:text="Reset"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent           
        app:layout_constraintTop_toBottomOf="@+id/TextView_numbers" />

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

EDIT: Here I found an error on Logcat:

01-26 18:40:15.710 32310-32310/karuntodroid.sieveoferatosthenes E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                  Process: karuntodroid.sieveoferatosthenes, PID: 32310
                                                                                  java.lang.NumberFormatException: For input string: ""
                                                                                      at java.lang.Integer.parseInt(Integer.java:533)
                                                                                      at java.lang.Integer.parseInt(Integer.java:556)
                                                                                      at karuntodroid.sieveoferatosthenes.MainActivity$2.onClick(MainActivity.java:38)
                                                                                      at android.view.View.performClick(View.java:5638)
                                                                                      at android.view.View$PerformClick.run(View.java:22444)
                                                                                      at android.os.Handler.handleCallback(Handler.java:751)
                                                                                      at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                      at android.os.Looper.loop(Looper.java:159)
                                                                                      at android.app.ActivityThread.main(ActivityThread.java:6139)
                                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
01-26 18:40:15.713 1390-2874/? W/ActivityManager:   Force finishing activity karuntodroid.sieveoferatosthenes/.MainActivity

Any help is appreciated. Thank you!

MikeT
  • 51,415
  • 16
  • 49
  • 68
Karuntos
  • 61
  • 2
  • 11

1 Answers1

2

You are setting the text in your EditText via edtPrime.setText(number, TextView.BufferType.EDITABLE); where your number variable is Integer.

If you look at the docs, this will try to fetch a resource with the identifier (id) of that number. (and you dont have that resource with that id...those resources are accessed for ex. via R.string.my_string)

What you need to do is convert the number to string and set it via setText(..)

JanBo
  • 2,925
  • 3
  • 23
  • 32