gpt4 book ai didi

java - 从数字 EditText 字段获取 NumberFormatException

转载 作者:行者123 更新时间:2023-11-29 07:14:12 25 4
gpt4 key购买 nike

我正在尝试创建一个 for 循环以将 View 添加到布局中。 for 循环工作正常(我尝试使用初始化变量)但我需要从 EditText 中获取一个整数。我使用了 try-catch 并且 LogCat 告诉我以下...

java.lang.NumberFormatException: invalid int "".

网站 developers.android.com 说这是由于错误地将字符串转换为整数,但我不明白我怎么会错误地从 EditText 获取数据。

这是我的代码...

public class UserPref2Activity extends Activity 

{
@Override

public void onCreate(Bundle savedInstanceState)
{
try
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
int i = Integer.parseInt(change);

int j; //iterates through the for loop

ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);

for(j=1;j<i;j++)
{
EditText name = new EditText(this);
name.setText("Name:");
EditText type = new EditText(this);
type.setText("Type:");
EditText bits = new EditText(this);
bits.setText("Bits:");
ll.addView(name);
ll.addView(type);
ll.addView(bits);
}
this.setContentView(sv);
}
catch (Exception e)
{
//sends actual error message to the log
Log.e("ERROR", "ERROR IN CODE:" + e.toString());
//prints out location of error
e.printStackTrace();
}
}
}

这是我的 XML 文件...

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/userLayout" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter number of sensors:" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:id="@+id/num_sensors" />

最佳答案

您的 EditText 最初有一个空的 String 作为它的值(即 "")。这显然不是一个数字,所以当您尝试调用 int i = Integer.parseInt(change); 时,它会给您一个错误提示。

有几种方法可以解决这个问题,但基本上可以归结为通过设置初始值来防止错误,或者检测空字符串并正确处理它。

为了防止错误发生...

EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
if (change.equals("")){ // detect an empty string and set it to "0" instead
change = "0";
}
int i = Integer.parseInt(change);

或者将 EditText 的初始值设置为 "0",但是这也会在 EditText 中显示值 0 在你的界面上而不是空的,所以它可能不适合所有目的......

<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:text="0"
android:id="@+id/num_sensors" />

如果你想检测错误并正确处理它,你会这样做......

EditText numSensors = (EditText) findViewById(R.id.num_sensors);
String change = numSensors.getText().toString();
int i = 0; // set it to 0 as the default
try {
i = Integer.parseInt(change);
}
catch (NumberFormatException e){}

这基本上设置了 i = 0 的值,并且只有在 try{} 代码没有给出错误时才会将其更改为不同的值。

关于java - 从数字 EditText 字段获取 NumberFormatException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11113238/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com