java - 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.");
}
}
}
}
解决方案
您应该 catch NumberFormatException
,因为您尝试将空行解析为数字。
try {
numArr[i] = Integer.parseInt(numArrF[i]);
} catch(NumberFormatException ex) {
numArr[i] = defaultValue; //Use default value if parsing failed
}
推荐阅读
- spring-boot - 将 Lucene 查询字符串直接从用户传递到 QueryParser 是否安全?
- javascript - setInterval 内部的 Promise
- laravel - 让我的“welcome.blade.php”识别在我的 Nuxt.js 文件夹中导入的 Vue 时遇到问题
- ios - 如何给标签数组元素的值?
- php - 为什么我的数组项没有传递到 foreach PHP for GraphQL 上的函数?
- angular - 一个空数组被推送到 localStorage 而不是数据
- clojure - Clojure:将值设置为键
- javascript - 在 javascript 中将数组转换为对象,与 d3 输出相同
- java - 将代码从 Java 更改为 Kotlin 后注释未正确转换
- angular - 在双花括号内呈现 HTML