android.content.res.Resources$NotFoundException: 字符串资源 ID #0x2b

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

我正在制作一个 Java 计算器应用程序。我的问题是当用户输入第二个数字时我的应用程序崩溃了。

它给出ResourceNotFoundException : String resource ID 0x2b.

我熟悉ResourceNotFoundException : String resource ID 0x0.

当用户点击任意数字时,onclick函数被调用。

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>

我正在显示 XML 代码:

    <?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>

这是 Java 文件:

    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);
        }
    }

我检查了在 onClick 方法上尝试拆分字符串时发生的错误。

我检查了很多问题,答案都建议使用 onClickListener,但我有很多数字按钮执行相同的功能。所以,我在 XML.

中使用了 android:onClick

如何解决我的错误? 如有任何帮助,我们将不胜感激!

 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);
}

用上面的方法就可以解决你的问题了。正则表达式语法问题。