简体   繁体   English

解析EditText时的Android NumberFormatException

[英]Android NumberFormatException when parsing EditText

I'm writing an app that takes in an input from the AddBook class, allows it to be displayed in a List , and then allows the user to Search for their book. 我正在编写一个应用程序,它接收来自AddBook类的输入,允许它显示在List ,然后允许用户搜索他们的书。 To this end, I'm creating a temporary EditText , binding it to the box where the user actually enters their search value, then (after ensuring that it is not empty) I compare what they've entered for the ISBN number with the ISBN numbers of each entry in the arrayList of <Book> custom objects, the list being named books. 为此,我正在创建一个临时的EditText ,将其绑定到用户实际输入其搜索值的框,然后(在确保它不为空之后)我将他们输入的ISBN号与ISBN进行比较<Book>自定义对象的arrayList中每个条目的arrayList ,该列表被命名为books。

Problem is, when I try to parse the EditText into an Int , it doesn't seem to work. 问题是,当我尝试将EditText解析为Int ,它似乎不起作用。 I first tried using toString() on the EditText , then using Integer.parseInt() on the result of that method, but it hasn't worked out, as the conversion is seemingly unsuccessful; 我首先尝试在EditText上使用toString() ,然后对该方法的结果使用Integer.parseInt() ,但它没有成功,因为转换似乎不成功;

All of the resources are in place and the code compiles properly, so those aren't the problems here. 所有资源都已到位且代码编译正确,因此这些不是问题。

  EditText myEdTx = (EditText) findViewById(R.id.bookName);

    if(myEdTx.getText().equals("")){Toast toast = Toast.makeText(this, "Please enter something for us to work with!", Toast.LENGTH_SHORT);
    toast.show();}
    else{
    //resume here
    for(int i=0; i<books.size(); i++)
    {
    Book tBook =  new Book(i, null, null);  tBook = books.get(i); String s=myEdTx.toString(); 
    int tInt = Integer.parseInt(s);`

To get the string representation of an EditText's content, use: 要获取EditText内容的字符串表示形式,请使用:

String s = myEdTx.getText().toString();

Using toString() directly on the EditText gives you something like: 直接在EditText上使用toString()可以得到如下内容:

android.widget.EditText{40d31450 VFED..CL ......I. 0,0-0,0}

...which is clearly not what you want here. ......这显然不是你想要的。

You assume the user inputs a number into the text field, but that is unsafe, as you only get a string text (which theoretically can contain non-numbers as well). 您假设用户在文本字段中输入一个数字,但这是不安全的,因为您只获得一个字符串文本(理论上也可以包含非数字)。 When I remember correctly, you can adjust a text field in android where a user only can input numbers, which should suit you more. 当我没记错的话,你可以在android中调整一个文本字段,用户只能输入数字,这应该更适合你。

NumberFormatException occurs when Integer.parse() is unable to parse a String as integer, so, its better to Handle this exception. Integer.parse()无法将String解析为整数时发生NumberFormatException ,因此,最好处理此异常。

   String s = myEdTx.getText().toString();
    try {
         int tInt = Integer.parseInt(s);
     } catch( NumberFormatException ex ) {
       //do something if s is not a number, maybe defining a default value.
       int tInt = 0;
     }

So the current String here you are trying to parse is with white space in the line and integer class unable to parse that white space. 因此,您尝试解析的当前String是行中的空格,而整数类无法解析该空格。 So use following code. 所以使用以下代码。

String s=myEdTx.getText().toString();     
int tInt = Integer.parseInt(s.trim());
String s = myEdtx.getText().toString().trim();

int iInt = Integer.parseInt(s);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM