java - Convert number in textview to int -


so, messing around java/android programming , right trying make basic calculator. hung on issue though. code have right getting number thats in textview , making int

charsequence value1 = gettext(r.id.textview); int num1 =  integer.parseint(value1.tostring()); 

and can tell second line causing error, im not sure why doing that. compiling fine, when tries run part of program crashes app. , thing thats in textview numbers

any advice?

i can provide more of code if necessary

you can read on usage of textview.

how declare it:

textview tv; 

initialize it:

tv = (textview) findviewbyid(r.id.textview); 

or:

tv = new textview(myactivity.this); 

or, if inflating layout,

tv = (textview) inflatedview.findviewbyid(r.id.textview); 

to set string tv, use tv.settext(some_string) or tv.settext("this_string"). if need set integer value, use tv.settext("" + 5) settext() overloaded method can handle string , int arguments.

to value tv use tv.gettext().

always check if parser can handle possible values textview.gettext().tostring() can supply. numberformatexception thrown if try parse empty string(""). or, if try parse ..

string tvvalue = tv.gettext().tostring();  if (!tvvalue.equals("") && !tvvalue.equals(......)) {     int num1 = integer.parseint(tvvalue); } 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -