My app crashes in the emulator when I press one of the buttons

0

I'm an absolute total beginner at coding please help? I'm trying to make a simple calculator and this is what the professor was telling me to do but it isn't working for some reason that I can't figure out...

This is my activity main XML. I put everything in linear layout because that's the only one we have learned in class so far.

activityMain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="15dp"
    tools:context=".MainActivity">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/num1Tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Number 1 : " />

        <EditText
            android:id="@+id/edNum1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <TextView
            android:id="@+id/num2Tv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Number 2 : " />

        <EditText
            android:id="@+id/edNum2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:ems="10"
            android:inputType="number" />
    </LinearLayout>


    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="+" />

    <Button
        android:id="@+id/btnSub"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="-" />

    <Button
        android:id="@+id/btnMul"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="*" />

    <Button
        android:id="@+id/btnDiv"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="/" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="10dp">

        <TextView
            android:id="@+id/tvResult"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Result : " />

        <TextView
            android:id="@+id/tvRes"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>


</LinearLayout>

And this is my Main Activity java. I think if something went wrong there's a high chance that it's in here because this part is when I started getting confused.

MainActivity.java

package com.example.calculatortask;

import android.support.v7.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);

        final EditText num1 = (EditText) findViewById(R.id.edNum1);
        final EditText num2 = (EditText) findViewById(R.id.edNum2);
        Button add = (Button) findViewById(R.id.btnAdd);
        Button sub = (Button) findViewById(R.id.btnSub);
        Button mul = (Button) findViewById(R.id.btnMul);
        Button div = (Button) findViewById(R.id.btnDiv);
        final TextView res = (TextView) findViewById(R.id.tvRes);

        add.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = num1.getText().toString();
                String str2 = num2.getText().toString();
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                int result = num1 + num2;

                res.setText(result);
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = num1.getText().toString();
                String str2 = num2.getText().toString();
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                int result = num1 - num2;

                res.setText(result);
            }
        });

        mul.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = num1.getText().toString();
                String str2 = num2.getText().toString();
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                int result = num1 * num2;

                res.setText(result);
            }
        });

        div.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String str1 = num1.getText().toString();
                String str2 = num2.getText().toString();
                int num1 = Integer.parseInt(str1);
                int num2 = Integer.parseInt(str2);
                int result = num1 / num2;

                res.setText(result);
            }
        });
    }
}

and this is my logcat.. I literally have no idea how to read this.. I heard that people find where the errors happened with this? so I'm putting it up.

Error:

E/.calculatortas: Invalid ID 0x00000009.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.calculatortask, PID: 3029
    android.content.res.Resources$NotFoundException: String resource ID #0x9
        at android.content.res.Resources.getText(Resources.java:348)
        at android.widget.TextView.setText(TextView.java:5831)
        at com.example.calculatortask.MainActivity$1.onClick(MainActivity.java:34)
        at android.view.View.performClick(View.java:6597)
        at android.view.View.performClickInternal(View.java:6574)
        at android.view.View.access$3100(View.java:778)
        at android.view.View$PerformClick.run(View.java:25885)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        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:858)
I/Process: Sending signal. PID: 3029 SIG: 9
Application terminated.
java
android
android-studio
asked on Stack Overflow Oct 8, 2020 by Luna • edited Oct 8, 2020 by Som

1 Answer

5

Replace res.setText(result) with res.setText(String.valueOf(result)). If you pass an int to setText, then it expects a resource id.

answered on Stack Overflow Oct 8, 2020 by PPartisan

User contributions licensed under CC BY-SA 3.0