简体   繁体   中英

String doesnt convert to int

I'm trying to get the text of an edittext and convert it to int but I'm facing this error:

android.content.res.Resources$NotFoundException: String resource ID #0x5

the code is very simple:

  temp=String.valueOf(editm.getText());

                minput = Integer.parseInt(temp);
                Toast.makeText(this, minput, Toast.LENGTH_SHORT).show();

temp is a string variable and minput is int type. also I've tried .tostring() & Integer.valueof()

You are trying to convert something to an int and then try to call Toast.makeText(Context context, int resId, int duration) . If the second argument is an int, it is expected to be a resource id.

The question is whether you want it to be converted to an int at all. Right now, you're using its value only to show it on a toast message, which in turn expects a string to be passed.

Toast.makeText(this, String.valueOf(minput), Toast.LENGTH_SHORT).show();

Try This would Fix it :

    temp=editm.getText().toString().trim();
    minput = Integer.parseInt(temp);
    Toast.makeText(this, minput, Toast.LENGTH_SHORT).show();

UPDATE :

It can issue for your toast : (try convert int to string in yout toast message)

Toast.makeText(this, minput.toString(), Toast.LENGTH_SHORT).show();

Try using toString

 temp= editm.getText().toString();

            minput = Integer.parseInt(temp);
            Toast.makeText(this, minput+"", Toast.LENGTH_SHORT).show();

and it still doesn't works then check the value you're getting there must be other issue.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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