Android Studio中@string引用失效,控件显示原始字符串求助
Hey, I’ve dealt with this frustrating issue a few times—let’s walk through the most likely fixes that usually resolve it:
Double-check your strings.xml setup
First, confirm your strings are properly defined inres/values/strings.xml(or the correct locale-specific subfolder if you’re localizing). Avoid special characters, spaces, or uppercase letters in resource names—stick to lowercase, numbers, and underscores. For example:<!-- This works --> <string name="app_name">myapp</string> <string name="action_sign_in">Sign In</string>A tiny typo here (like
app_nameeinstead ofapp_name) will make Android fall back to showing the raw@string/reference.Clean and rebuild your project
Build cache corruption is super common here. Head to Android Studio’s menu:Build > Clean Project, thenBuild > Rebuild Project. This forces the IDE to recompile all your resources from scratch, which often fixes stale or mislinked references.Check for resource conflicts
If you’re using third-party libraries or multiple modules, there might be a duplicate resource name (e.g., another library definesapp_nametoo). To check this, right-click yourresfolder and selectOpen Resource Manager—look for entries marked with a warning icon. If you find a conflict, rename your string (likemy_app_name) to avoid the clash.Verify your references are 100% correct
It sounds obvious, but make sure you didn’t accidentally hardcode the reference text. For example,android:text="@string/action_sign_in"is correct—but if you missed the@string/part (or had a typo like@string/action_signin), it’ll show the raw text instead of the actual string value.Check your build.gradle configuration
Make sure your module’sbuild.gradle(orbuild.gradle.kts) is correctly pointing to yourresfolder. The sourceSets should look like this:android { sourceSets { main { res.srcDirs = ['src/main/res'] } } }If this is misconfigured, Android won’t pick up your string resources at all.
Test on a different device/emulator
Sometimes emulators hold onto cached resources that don’t update. Try running the app on a physical device or creating a brand new emulator to rule out this edge case.
Most of the time, one of these steps will fix the problem—start with cleaning the project and checking for typos, since those are the most frequent culprits!
内容的提问来源于stack exchange,提问作者Kaigo

