amff
amff

Reputation: 115

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()

Upvotes: 1

Views: 90

Answers (2)

MC Emperor
MC Emperor

Reputation: 22967

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();

Upvotes: 3

MoHammaD ReZa DehGhani
MoHammaD ReZa DehGhani

Reputation: 575

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();

Upvotes: 0

Related Questions