java - 关于 String 和 Int 之间转换的一些疑问
问题描述
好的,事情就是这样。我正在与 android studio 一起完成我学位的最终项目。我的职责之一是创建一个Arraylist
其最大尺寸必须是用户介绍的数字!我想它看起来很简单,但我卡在某个点上,我怀疑是大小值在和之间的String
转换Int
。
具体来说,我试图从 and 中获取用户的号码editText
,转换为 StringgetText().toString()
然后我用它来转换为 intInteger.parseInt()
但是当我在设备中运行它时,应用程序立即崩溃。我也已经尝试valueOf
过,也改变int
了Integer
(对不起,我不知道有什么区别,我从 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);
}
}
此外,如果您知道一个可以进行统计数学运算(如典型偏差、算术平均值等)的优秀库......对于我的最终项目来说将是完美的。
太感谢了 !
解决方案
推荐阅读
- azure - 如何使用 2 因素身份验证通过 Powershell 登录 Azure?
- python - 如何修复客户端/服务器中的错误(重新提问)
- sas - PROC SCAPROC 不读取使用 PROC SQL 创建的视图
- mysql - MySQL RDS 1114 (HY000):表 '/rdsdbdata/tmp/#sql4c54_f_a' 已满
- c# - 将产品版本添加到 SpecFlow+ Runner ReportTemplate.CSHTML
- android - 将视图的顶部和底部约束到相同的准则以集中它,它应该工作吗?
- html - 如何将 div 与文本垂直对齐
- php - 教义一对一关系,主表有两列与一张表相关两次
- react-native - Expo(React Native)无法加载所有资产错误
- python - 如何在numpy中将矩阵与代数相乘