Home > Mobile >  android.content.res.Resources$NotFoundException: String resource ID #0x2b
android.content.res.Resources$NotFoundException: String resource ID #0x2b

Time:05-03

I'm making an Java application that is Calculator. My problem is that my app crashes when user enter the second number.

It gives ResourceNotFoundException : String resource ID 0x2b.

I am familiar to ResourceNotFoundException : String resource ID 0x0.

When user clicks any number, onclick function is called.

Strings.xml

    <resources>
        <string name="app_name">Calculator</string>
        <string name="one">1</string>
        <string name="two">2</string>
        <string name="three">3</string>
        <string name="four">4</string>
        <string name="five">5</string>
        <string name="six">6</string>
        <string name="seven">7</string>
        <string name="eight">8</string>
        <string name="nine">9</string>
        <string name="zero">0</string>
        <string name="minus">-</string>
        <string name="plus"> </string>
        <string name="multiply">x</string>
        <string name="divide">÷</string>
        <string name="equal_to">=</string>
        <string name="backspace">⌫</string>
    </resources>

I'm showing the XML code:

    <?xml version="1.0" encoding="utf-8"?>
    <androidx.constraintlayout.widget.ConstraintLayout        
        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/display"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:layout_gravity="end"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="36dp"
        android:text=""
        android:textAlignment="viewEnd"
        android:textSize="80sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@ id/result"
        android:layout_width="match_parent"
        android:layout_height="80dp"
        android:layout_gravity="end"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginTop="156dp"
        android:background="@color/teal_200"
        android:text=""
        android:textAlignment="viewEnd"
        android:textSize="50sp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@ id/text0"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginBottom="4dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/zero"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/equalTo"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginBottom="4dp"
        android:background="@color/white"
        android:padding="10dp"
        android:text="@string/equal_to"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/back"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="204dp"
        android:layout_marginLeft="204dp"
        android:layout_marginBottom="4dp"
        android:background="@color/white"
        android:padding="10dp"
        android:text="@string/backspace"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/minus"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="304dp"
        android:layout_marginLeft="304dp"
        android:layout_marginBottom="4dp"
        android:background="@color/white"
        android:onClick="setOperator"
        android:padding="10dp"
        android:text="@string/minus"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text1"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginBottom="104dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/one"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text2"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginBottom="104dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/two"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text3"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="204dp"
        android:layout_marginLeft="204dp"
        android:layout_marginBottom="104dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/three"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/plus"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="304dp"
        android:layout_marginLeft="304dp"
        android:layout_marginBottom="104dp"
        android:background="@color/white"
        android:onClick="setOperator"
        android:padding="10dp"
        android:text="@string/plus"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text4"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginBottom="204dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/four"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text5"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginBottom="204dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/five"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text6"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="204dp"
        android:layout_marginLeft="204dp"
        android:layout_marginBottom="204dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/six"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/multiply"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="304dp"
        android:layout_marginLeft="304dp"
        android:layout_marginBottom="204dp"
        android:background="@color/white"
        android:onClick="setOperator"
        android:padding="10dp"
        android:text="@string/multiply"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text7"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="4dp"
        android:layout_marginLeft="4dp"
        android:layout_marginBottom="304dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/seven"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text8"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="104dp"
        android:layout_marginLeft="104dp"
        android:layout_marginBottom="304dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/eight"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/text9"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="204dp"
        android:layout_marginLeft="204dp"
        android:layout_marginBottom="304dp"
        android:background="@color/white"
        android:onClick="setNumber"
        android:padding="10dp"
        android:text="@string/nine"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    <Button
        android:id="@ id/divide"
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:layout_marginStart="304dp"
        android:layout_marginLeft="304dp"
        android:layout_marginBottom="304dp"
        android:background="@color/white"
        android:onClick="setOperator"
        android:padding="10dp"
        android:text="@string/divide"
        android:textColor="@color/black"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        tools:ignore="UsingOnClickInXml" />

    </androidx.constraintlayout.widget.ConstraintLayout>

Here is the Java file:

    package com.java.calculator;

    import android.os.Build;
    import android.os.Bundle;

    import androidx.annotation.RequiresApi;
    import androidx.appcompat.app.AppCompatActivity;

    import android.text.InputType;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import java.util.Arrays;

    public class MainActivity extends AppCompatActivity {
        public Button button, erase, result;
        public String num1, num2, displayText;
        public EditText display;
        public TextView rView;
        char operator;
        String[] nos;
        public int number1, number2, resultInt;

        public Operations operations;

        @RequiresApi(api = Build.VERSION_CODES.CUPCAKE)
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            display = findViewById(R.id.display);
            rView = findViewById(R.id.result);
            erase = findViewById(R.id.back);
            result = findViewById(R.id.equalTo);
            
            display.setInputType(InputType.TYPE_NULL);
            operations = new Operations();

            erase.setOnClickListener(v -> {
                try {
                    displayText = display.getText().toString();
                    if (displayText.length() > 0) {
                        displayText = displayText.substring(0, displayText.length() - 1);
                    }
                    display.setText(displayText);
                } catch (NullPointerException npe) {
                    //do nothing
                }
            });

            result.setOnClickListener(v -> {
                Log.d("number1 is", String.valueOf(number1));
                Log.d("number2 is", String.valueOf(number2));
                switch (operator) {
                    case ' ':
                        resultInt = number1   number2;
                        break;
                    case '-':
                        resultInt = number1 - number2;
                        break;
                    case 'x':
                        resultInt = number1 * number2;
                        break;
                    case '÷':
                        resultInt = number1 / number2;
                        break;
                }
                rView.setText(String.valueOf(resultInt));
            });
        }

        public void setNumber(View view) {
            button = (Button)view;
            displayText = display.getText().toString();
            char[] characters = displayText.toCharArray();
            if (displayText.equals("") || ((!(new String(characters).contains(Character.toString(' ')))) && (!(new String(characters).contains(Character.toString('-')))) && (!(new String(characters).contains(Character.toString('x')))) && (!(new String(characters).contains(Character.toString('÷')))))) {
                num1 = button.getText().toString();
                displayText = displayText   num1;
            } else {
                num2 = button.getText().toString();
                displayText = displayText   num2;
                nos = displayText.split(String.valueOf(operator));
                Log.d("array", "arr: "   Arrays.toString(nos));
                number1 = Integer.parseInt(Arrays.asList(nos).indexOf(0));
                number2 = Integer.parseInt(Arrays.asList(nos).indexOf(1));
            }
            display.setText(displayText);
        }

        public void setOperator(View view) {
            button = (Button) view;
            String strOperator = button.getText().toString();
            operator = strOperator.charAt(0);

            displayText = display.getText().toString()   operator;
            display.setText(displayText);
        }
    }

I checked that error occurring when tried to split string on onClick method.

I check many of questions and the answers were suggesting to use onClickListener but I have many number buttons that are performing the same function. So, I used android:onClick in XML.

How can solve my error? Any help would be appreciated!

CodePudding user response:

 public void setNumber(View view) {
    button = (Button)view;
    displayText = display.getText().toString().trim();
    char[] characters = displayText.toCharArray();
    if (displayText.equals("") || ((!(new String(characters).contains(Character.toString(' ')))) && (!(new String(characters).contains(Character.toString('-')))) && (!(new String(characters).contains(Character.toString('x')))) && (!(new String(characters).contains(Character.toString('÷')))))) {
        num1 = button.getText().toString().trim();
        displayText = displayText   num1;
    } else {
        num2 = button.getText().toString().trim();
        displayText = displayText   num2;
        nos = displayText.split(Pattern.quote(operator.toString()));
        Log.d("array", "arr: "   Arrays.toString(nos));
        number1 = Integer.parseInt(nos[0]);
        number2 = Integer.parseInt(nos[1]);
    }
    display.setText(displayText);
}

Use the above method it will solve your issue. The issue with regular expression syntax.

  • Related