The support-annotations library now supports annotation for @StringRes (and a lot more). I was hoping this issue would be fixed now, but it is still possible to add a int value to a setText method.
Also checked by using these annotations, and in Android Studio you get a nice message: *Expexted resource of type string* Does this mean we can expect a solution shortly? On Thursday, March 7, 2013 4:36:43 AM UTC+1, Tor Norbye wrote: > > Thanks for the idea! > > I've filed the following issue to track this request: > https://code.google.com/p/android/issues/detail?id=52906 > > It won't be trivial to implement, but should be useful. If you have > examples of how you ran into this bug, can you add it here or better yet > attach it to that issue? I'd like to make sure that if there are particular > patterns that people easily accidentally call, that the flow analysis will > catch those. > > We already look for accidental color usages, like this one: > textView.setTextColor(android.R.color.red) > which is simple to make, since R.color.red is an int and it looks like the > method should accept it. > > What are the scenarios you run into this with strings? > > -- Tor > > > > On Tue, Feb 26, 2013 at 6:42 AM, Richard Critten < > [email protected] <javascript:>> wrote: > >> Unless, for example, getSomeValueFromSomewhere() was returning a string >> id as the result of a case statement - so the text could be dynamically set. >> >> >> On Tuesday, February 26, 2013 1:39:48 PM UTC, Özcan Kaymak wrote: >>> >>> After making this error twice today, it seemed to me it would be a >>> useful new lint check. >>> On a TextView (a Button also) there is a method setText, where you >>> normally use a string or a string-resource. >>> A string-resource is actually an int, so this error is easily made: >>> >>> TextView textView = (TextView)findViewByID(R.id.textview); >>> textView.setText(getSomeValueFromSomewhere()); >>> >>> This will compile, but will most likely crash on runtime if >>> getSomeValueFromSomewhere() returns an int. >>> >>> It would be nice if Lint could check for this error by checking if the >>> int-value corresponds to a R.string.-field. >>> >> -- >> You received this message because you are subscribed to the Google Groups >> "adt-dev" group. >> To unsubscribe from this group and stop receiving emails from it, send an >> email to [email protected] <javascript:>. >> For more options, visit https://groups.google.com/groups/opt_out. >> >> >> > > -- You received this message because you are subscribed to the Google Groups "adt-dev" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.
