-1

I am a beginner at Android Studio, I tried emulate my application that multiplies two numbers however whenever I am emulating the application on a device, and clicked the button to calculate the two numbers, the application kept on stopping. The device that I am using is Nexus_5x API 26. These are the errors that I encountered in the Run component.

enter image description here

ERROR:

E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length
E/SpannableStringBuilder: SPAN_EXCLUSIVE_EXCLUSIVE spans cannot have a zero length


E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.sample, PID: 5372
    java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{c3629d8 VFED..CL. ........ 265,118-815,244 #7f0800b8 app:id/firstnumber}"
        at java.lang.Integer.parseInt(Integer.java:608)
        at java.lang.Integer.parseInt(Integer.java:643)
        at com.example.sample.MainActivity$1.onClick(MainActivity.java:26)
        at android.view.View.performClick(View.java:6256)
        at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1119)
        at android.view.View$PerformClick.run(View.java:24701)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)



activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">

<EditText
    android:id="@+id/firstnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="45dp"
    android:ems="10"
    android:hint="Enter First Number"
    android:inputType="textPersonName"
    android:minHeight="48dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>

<EditText
    android:id="@+id/secondnumber"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="17dp"
    android:ems="10"
    android:hint="Enter Second Number"
    android:inputType="textPersonName"
    android:minHeight="48dp"
    app:layout_constraintStart_toStartOf="@+id/firstnumber"
    app:layout_constraintTop_toBottomOf="@+id/firstnumber"/>

<Button
    android:id="@+id/calculate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="21dp"
    android:text="Calculate"
    app:layout_constraintEnd_toEndOf="@+id/secondnumber"
    app:layout_constraintStart_toStartOf="@+id/secondnumber"
    app:layout_constraintTop_toBottomOf="@+id/secondnumber"/>

<TextView
    android:id="@+id/result"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="236dp"
    android:text="Result"
    android:textSize="96sp"
    android:textStyle="bold"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>

main_activity.java

package com.example.sample;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button calculate = (Button) findViewById(R.id.calculate);
    calculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            EditText firstnumber = (EditText) findViewById(R.id.firstnumber);
            EditText secondnumber = (EditText) findViewById(R.id.secondnumber);
            TextView calculate = (TextView) findViewById(R.id.calculate);

            int var1 = Integer.parseInt(String.valueOf(firstnumber));
            int var2 = Integer.parseInt(String.valueOf(firstnumber));
            int result = Integer.parseInt(String.valueOf(calculate));

            result = var1 * var2;

            calculate.setText(result + "");

        }
    });
}

}

Fureeish
  • 12,533
  • 4
  • 32
  • 62
Kirby Kode
  • 19
  • 4

2 Answers2

1

Change these lines

            EditText firstnumber = (EditText) findViewById(R.id.firstnumber);
            EditText secondnumber = (EditText) findViewById(R.id.secondnumber);
            TextView calculate = (TextView) findViewById(R.id.calculate);

            int var1 = Integer.parseInt(String.valueOf(firstnumber));
            int var2 = Integer.parseInt(String.valueOf(firstnumber));
            int result = Integer.parseInt(String.valueOf(calculate));

            result = var1 * var2;

            calculate.setText(result + "");

to

            EditText Firstnumber = findViewById(R.id.firstnumber);
            EditText Secondnumber =  findViewById(R.id.secondnumber);
            TextView Result =  findViewById(R.id.result);

            int var1 = Integer.parseInt(Firstnumber.getText().toString());
            int var2 = Integer.parseInt(Secondnumber.getText().toString());
            int result ;

            result = var1 * var2;

            Result.setText(result + "");

Also be sure the input should be Numbers not Letters ...

Dreamer
  • 517
  • 1
  • 4
  • 11
  • why do i need to change the variable from calculate to result ? and what is the difference between .getText().toString() and String.valueOf? – Kirby Kode Apr 07 '22 at 09:30
  • Calculate is Button .. Result is TextView .. You posted the layout so i could see how you built it .. – Dreamer Apr 07 '22 at 11:19
  • For the difference you can check here.. Anyways you need to use getText() in both cases.. https://stackoverflow.com/questions/27465731/string-valueof-vs-object-tostring. – Dreamer Apr 07 '22 at 11:21
0

The very first line of your error tells us everything:

java.lang.NumberFormatException: For input string: "androidx.appcompat.widget.AppCompatEditText{c3629d8 VFED..CL. ........ 265,118-815,244 #7f0800b8 app:id/firstnumber}"

Pay attention to this part: "androidx.appcompat.widget.AppCompatEditText{c3629d8 VFED..CL. ........ 265,118-815,244 #7f0800b8 app:id/firstnumber}".

This is what you get when you are trying to do:

EditText firstnumber = (EditText) findViewById(R.id.firstnumber);
int var1 = Integer.parseInt(String.valueOf(firstnumber));

That's because String.valueOf(firstnumber) will not yield the text that EditText was holding. You need to do firstnumber.getText().toString() to extract the text that user was inputting.

Fureeish
  • 12,533
  • 4
  • 32
  • 62