Tsunaze
Tsunaze

Reputation: 3254

avoid non constant expression in android

There is a new thing since ADT 14, it's called non-constant expression: migration necessary. Which cause that I can't do stuff like this:

TextView tv = (TextView) findViewById(R.id.text);

Or this:

getRessources().getDrawable(R.drawable.icon);

What I have to do to make it work now?

Upvotes: 1

Views: 1770

Answers (2)

mudit_sen
mudit_sen

Reputation: 1510

In Eclipse, Click On Project -> Properties -> Android

and uncheck if true "Is Library" option.

Upvotes: 1

james
james

Reputation: 26271

You must refactor your code since in library projects, the R fields are no longer constants(not final). See http://tools.android.com/tips/non-constant-fields for details and examples on how to fix the issue.

Basically, you need to change your switch statements to if-else since you cannot switch over a non-constant.

Also, this is only for library projects. regular android projects do not require this change.

After you make the changes be sure to clean and refresh your project as some errors/warnings may linger.

Upvotes: 3

Related Questions