首页 > 解决方案 > Android 应用程序仅在真实设备上引发错误

问题描述

该应用程序将二进制代码转换为其他数字系统。

在模拟器上一切正常,但是当您按下应用程序中的按钮时,一切都会关闭并给出指向此行的错误 (58)

numArr[i] = Integer.parseInt(numArrF[i]);

我认为这是库版本的问题。或者它们在真实设备上完全不存在。我不明白这个问题。

这是主要活动的代码:

package com.example.myct;

import android.annotation.SuppressLint;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;

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

import java.util.Arrays;
import java.util.Scanner;
import java.util.stream.Stream;

import static java.lang.Integer.parseInt;
import static java.util.Arrays.*;
import static java.util.Arrays.stream;

public class MainActivity extends AppCompatActivity {

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

    @SuppressLint("SetTextI18n")
    @RequiresApi(api = Build.VERSION_CODES.N)
    public void onMyButtonClick(View view) {
        Spinner mySpinner1 = (Spinner) findViewById(R.id.spinner1);
        String spinNum1 = mySpinner1.getSelectedItem().toString();

        Spinner mySpinner2 = (Spinner) findViewById(R.id.spinner2);
        String spinNum2 = mySpinner2.getSelectedItem().toString();

        EditText number = (EditText) findViewById(R.id.editTextNumber);
        String eNum = number.getText().toString();

        TextView Field = (TextView) findViewById(R.id.Field);
        String fieldI = Field.getText().toString();
        Field.setText("");

        if (spinNum1.equals("2")) {

            if (spinNum2.equals("2")) {
                Field.setText(eNum);
            }

            //преобразование строки eNum в int массив
            String[] numArrF = eNum.split("");
            int numArr[] = new int[numArrF.length];
            for (int i = 0; i < numArrF.length; i++) {
                numArr[i] = Integer.parseInt(numArrF[i]);
            }
            //////////////////

            boolean solve = true;
            //test null and one
            for(int i = numArr.length-1; i >= 0; i--){
                if (numArr[i] != 1 && numArr[i] != 0) {
                    solve = false;
                    Log.d("solve", "false");
                }
            }
            //////////////////

            //если в массиве все элементы равны только нулю и еденице
            if (solve) {


                ////////////////////////////////////////////////////////////////////////////////////
                if (spinNum2.equals("2")) {
                    Field.setText(eNum);
                }
                ////////////////////////////////////////////////////////////////////////////////////


                ////////////////////////////////////////////////////////////////////////////////////
                else if (spinNum2.equals("8")){

                    String[] arrStr = eNum.replaceAll("(.{3})","$1 ").split(" ");

                    for (int i = arrStr.length-1; i < arrStr.length; i++){
                        if (arrStr[i].length() < 3){
                            arrStr[i] = "0" + arrStr[i];
                        }
                    }

                    for(int i = 0;  i < arrStr.length; i++ ){

                        String str = arrStr[i];


                        if(str.equals("000")){
                            String other = Field.getText().toString();
                            arrStr[i] = "0";
                            if(i == 0){
                                Field.setText("000 = 0");
                            }
                            else {Field.setText(other+", 000 = 0");}
                        }
                        else if(str.equals("001")){

                            arrStr[i] = "1";
                            String other = Field.getText().toString();
                            if(i == 0){
                                Field.setText("001 = 1");
                            }
                            else {Field.setText(other+", 001 = 1");}
                        }
                        else if(str.equals("010")){
                            String other = Field.getText().toString();
                            arrStr[i] = "2";
                            if(i == 0){
                                Field.setText("010 = 2");
                            }
                            else {Field.setText(other+", 010 = 2");}
                        }
                        else if(str.equals("011")){
                            String other = Field.getText().toString();
                            arrStr[i] = "3";
                            if(i == 0){
                                Field.setText("011 = 3");
                            }
                            else {Field.setText(other+", 011 = 3");}
                        }
                        else if(str.equals("100")){
                            String other = Field.getText().toString();
                            arrStr[i] = "4";
                            if(i == 0){
                                Field.setText("100 = 4");
                            }
                            else {Field.setText(other+", 100 = 4");}
                        }
                        else if(str.equals("101")){
                            String other = Field.getText().toString();
                            arrStr[i] = "5";
                            if(i == 0){
                                Field.setText("101 = 5");
                            }
                            else {Field.setText(other+", 101 = 5");}
                        }
                        else if(str.equals("110")){
                            String other = Field.getText().toString();
                            arrStr[i] = "6";
                            if(i == 0){
                                Field.setText("110 = 6");
                            }
                            else {Field.setText(other+", 110 = 6");}
                        }
                        else if(str.equals("111")){
                            String other = Field.getText().toString();
                            arrStr[i] = "7";
                            if(i == 0){
                                Field.setText("111 = 7");
                            }
                            else {Field.setText(other+", 111 = 7");}
                        }

                    }

                }
                ////////////////////////////////////////////////////////////////////////////////////


                ////////////////////////////////////////////////////////////////////////////////////
                else if (spinNum2.equals("10")) {

                    int sum = 0;

                    int[] comp = new int[numArr.length];

                    int a = 0;
                    for (int i = numArr.length - 1; i >= 0; i--) {
                        //записываем в ячейку что она равна самой себе умноженной на 2 в степени i
                        int camper = (int) Math.pow(2, a);

                        comp[i] = numArr[i] * camper;

                        //сумма всех ячеек
                        sum += comp[i];

                        if (i == 0) {
                            String otherA = Field.getText().toString();
                            Field.setText(otherA + numArr[i] + " * 2^" + a + " = " + sum);
                        } else {
                            String otherB = Field.getText().toString();
                            Field.setText(otherB + numArr[i] + " * 2^" + a + " + ");
                        }
                        a++;
                    }
                }
                ////////////////////////////////////////////////////////////////////////////////////


                ////////////////////////////////////////////////////////////////////////////////////
                else if (spinNum2.equals("16")){

                    String[] arrStr = eNum.replaceAll("(.{4})","$1 ").split(" ");

                    for (int i = arrStr.length-1; i < arrStr.length; i++){
                        if (arrStr[i].length() < 4){
                            arrStr[i] = "0" + arrStr[i];
                        }
                    }

                    for(int i = 0;  i < arrStr.length; i++ ){

                        String str = arrStr[i];


                        if(str.equals("0000")){
                            String other = Field.getText().toString();
                            arrStr[i] = "0";
                            if(i == 0){
                                Field.setText("0000 = 0");
                            }
                            else {Field.setText(other+", 0000 = 0");}
                        }
                        else if(str.equals("0001")){

                            arrStr[i] = "1";
                            String other = Field.getText().toString();
                            if(i == 0){
                                Field.setText("0001 = 1");
                            }
                            else {Field.setText(other+", 0001 = 1");}
                        }
                        else if(str.equals("0010")){
                            String other = Field.getText().toString();
                            arrStr[i] = "2";
                            if(i == 0){
                                Field.setText("0010 = 2");
                            }
                            else {Field.setText(other+", 0010 = 2");}
                        }
                        else if(str.equals("0011")){
                            String other = Field.getText().toString();
                            arrStr[i] = "3";
                            if(i == 0){
                                Field.setText("0011 = 3");
                            }
                            else {Field.setText(other+", 0011 = 3");}
                        }
                        else if(str.equals("0100")){
                            String other = Field.getText().toString();
                            arrStr[i] = "4";
                            if(i == 0){
                                Field.setText("0100 = 4");
                            }
                            else {Field.setText(other+", 0100 = 4");}
                        }
                        else if(str.equals("0101")){
                            String other = Field.getText().toString();
                            arrStr[i] = "5";
                            if(i == 0){
                                Field.setText("0101 = 5");
                            }
                            else {Field.setText(other+", 0101 = 5");}
                        }
                        else if(str.equals("0110")){
                            String other = Field.getText().toString();
                            arrStr[i] = "6";
                            if(i == 0){
                                Field.setText("0110 = 6");
                            }
                            else {Field.setText(other+", 0110 = 6");}
                        }
                        else if(str.equals("0111")){
                            String other = Field.getText().toString();
                            arrStr[i] = "7";
                            if(i == 0){
                                Field.setText("0111 = 7");
                            }
                            else {Field.setText(other+", 0111 = 7");}
                        }
                        else if(str.equals("1000")){
                            String other = Field.getText().toString();
                            arrStr[i] = "8";
                            if(i == 0){
                                Field.setText("1000 = 8");
                            }
                            else {Field.setText(other+", 1000 = 8");}
                        }
                        else if(str.equals("1001")){
                            String other = Field.getText().toString();
                            arrStr[i] = "9";
                            if(i == 0){
                                Field.setText("1001 = 9");
                            }
                            else {Field.setText(other+", 1001 = 9");}
                        }
                        else if(str.equals("1010")){
                            String other = Field.getText().toString();
                            arrStr[i] = "A";
                            if(i == 0){
                                Field.setText("1010 = A");
                            }
                            else {Field.setText(other+", 1010 = A");}
                        }
                        else if(str.equals("1011")){
                            String other = Field.getText().toString();
                            arrStr[i] = "B";
                            if(i == 0){
                                Field.setText("1011 = B");
                            }
                            else {Field.setText(other+", 1011 = B");}
                        }
                        else if(str.equals("1100")){
                            String other = Field.getText().toString();
                            arrStr[i] = "C";
                            if(i == 0){
                                Field.setText("1100 = C");
                            }
                            else {Field.setText(other+", 1100 = C");}
                        }
                        else if(str.equals("1101")){
                            String other = Field.getText().toString();
                            arrStr[i] = "D";
                            if(i == 0){
                                Field.setText("1101 = D");
                            }
                            else {Field.setText(other+", 1101 = D");}
                        }
                        else if(str.equals("1110")){
                            String other = Field.getText().toString();
                            arrStr[i] = "E";
                            if(i == 0){
                                Field.setText("1110 = E");
                            }
                            else {Field.setText(other+", 1110 = E");}
                        }
                        else if(str.equals("1111")){
                            String other = Field.getText().toString();
                            arrStr[i] = "F";
                            if(i == 0){
                                Field.setText("1111 = F");
                            }
                            else {Field.setText(other+", 1111 = F");}
                        }

                    }
                }
                ////////////////////////////////////////////////////////////////////////////////////


            }
            else {
                Field.setText("Dont binary.");
            }
        }
    }
}

标签: javaandroidandroid-emulator

解决方案


您应该 catch NumberFormatException,因为您尝试将空行解析为数字。

try {
    numArr[i] = Integer.parseInt(numArrF[i]);
} catch(NumberFormatException ex) {
    numArr[i] = defaultValue; //Use default value if parsing failed
}

推荐阅读