Reputation: 1
i checked this web site on how to convert from string to integer type in java(android).... one of suggestion was to use (integer.parseint) i used it and when i run my application it says my app has stopped working my code below .
public void clickable (View view){
EditText mytext = (EditText) findViewById(R.id.Creat);
int number = Integer.parseInt(mytext.getText().toString());
Toast.makeText(getApplicationContext(), number ,Toast.LENGTH_LONG).show();
}
i cant figure out what is the problem with the code ?!
Upvotes: 0
Views: 69
Reputation: 1238
There are many implementations of Toast.makeText
. As you are passing an int
as the second argument, the following implementation will execute:
Toast makeText(Context context, @StringRes int resId, @Duration int duration)
This implementation will throw a ResourcesNotFoundException
if it cannot find a resource with the id of resId
.
To output number
as a String
you need to convert it:
Toast.makeText(getApplicationContext(), String.valueOf(number), Toast.LENGTH_LONG).show();
Upvotes: 0
Reputation: 859
Declare EditText mytext variable as a global variable and then initialize it in Oncreate() method of your Activity. Then your clickable method looks like this:
public void clickable (View view){
int number = Integer.parseInt(mytext.getText().toString());
Toast.makeText(getApplicationContext(), mytext.getText().toString(), Toast.LENGTH_LONG).show();
}
Obeserve Toast.makeText() method's second argument is the resource id of the string resource to use or it can be formatted text. In your code you have passed an integer as a resource id which does not exist. So you get ResourcesNotFoundException.
Upvotes: 1
Reputation: 21
What string are you passing into the Integer.parseInt()? If it's not an integer, your program will experience a NumberFormatException. I'm not sure if that's the issue here, but I'm not sure what you're passing into the Integer.parseInt().
Upvotes: 0