开发者

string defined but unresolved?

开发者 https://www.devze.com 2023-03-15 05:33 出处:网络
I have strings defined in my st开发者_如何学运维rings.xml as: <resources> <string name=\"edit_student\">Edit Student</string>

I have strings defined in my st开发者_如何学运维rings.xml as:

<resources>
<string name="edit_student">Edit Student</string>
<string name="add_activity">Add Activity</string>
<string name="act_name">Activity</string>
</resources>

But when I reference it with ..

setTitle(R.string.add_activity);

..it fails with error.

add_activity cannot be resolved or is not a field

However a similar statement in another place works just fine

setTitle(R.string.edit_student);

What's wrong with the first one that doesn't but the second does?


You can't use the string resources directly. Use it as following:

setTitle(context.getString(R.string.resource_name));


Aside from verifying your imports (for the R class), try doing a clean build. Sometimes the R class is not regenerated correct


Check the import statements of your class file, from which package R is imported.


setTitle(getResources().getString(R.string.add_activity));
0

精彩评论

暂无评论...
验证码 换一张
取 消