首页 > 解决方案 > 关于 String 和 Int 之间转换的一些疑问

问题描述

好的,事情就是这样。我正在与 android studio 一起完成我学位的最终项目。我的职责之一是创建一个Arraylist其最大尺寸必须是用户介绍的数字!我想它看起来很简单,但我卡在某个点上,我怀疑是大小值在和之间的String转换Int

具体来说,我试图从 and 中获取用户的号码editText,转换为 StringgetText().toString()然后我用它来转换为 intInteger.parseInt()但是当我在设备中运行它时,应用程序立即崩溃。我也已经尝试valueOf过,也改变intInteger(对不起,我不知道有什么区别,我从 0 开始自己研究这一切)并且仍然可以立即关闭。我在这里搜索了很多,但我找不到如此具体的解决方案,所以如果有人可以帮助我,我将非常感激。这是我的代码!希望能帮助到你!

package com.example.listwithrecyclerview;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

public class MainActivity extends AppCompatActivity {

 Integer num;
 String Snum;

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

    EditText ET1 = findViewById(R.id.editText);
    Snum = ET1.getText().toString();
    num = Integer.parseInt(Snum);

    //also if i replace Snum by a number like:"23" in this last line, it works, so i guess it could 
    //be the way i put that variable inside the parseInt().

}

public void SendList (View view){
    Intent A = new Intent(this, List.class);
    A.putExtra("size", num);
    startActivity(A);
}

}

此外,如果您知道一个可以进行统计数学运算(如典型偏差、算术平均值等)的优秀库......对于我的最终项目来说将是完美的。

太感谢了 !

标签: javaandroidarraysandroid-studiodata-conversion

解决方案


推荐阅读