Android开发:res下raw文件夹存在但R.raw.Gravity代码报红求助
Hey there, let's work through why R.raw.Gravity is throwing that frustrating red error—this is a super common gotcha in Android development, so we'll get it sorted quickly! Here are the most likely fixes to try:
Fix 1: Rename your resource file to lowercase
Android's resource system has strict filename rules: only lowercase letters, numbers, and underscores are allowed. Uppercase characters like the "G" in "Gravity" break R class generation. Rename your file togravity.mp3(or whatever your file extension is) and update your code to:ourSound = MediaPlayer.create(this, R.raw.gravity);Fix 2: Rebuild your project
Sometimes Android Studio fails to auto-sync new resources to the R class. Try these steps:- Go to
Build > Clean Projectto clear outdated build files. - Run
Build > Rebuild Projectto regenerate the R class from scratch. - You can also click the "Sync Project with Gradle Files" button (the elephant icon in the top toolbar) to force a sync.
- Go to
Fix 3: Check your R class import
It's easy to accidentally import the wrong R class. Look at the top of your activity/fragment code—make sure you're importing your app's R class, not the system default:import com.your.app.package.name.R; // Correct // import android.R; // Wrong—delete this if it's presentFix 4: Verify the raw folder's location
Double-check that yourrawfolder is in the correct path:src/main/res/raw. If it's in the wrong directory (like insidesrc/main/java) or misspelled (e.g.,raws), the resource won't be detected.Fix 5: Check for a corrupted audio file
If theGravityfile itself is damaged or in an unsupported format, Android might skip generating its resource entry. Try replacing it with a known-working audio file (like a standard MP3) and see if the error disappears.
内容的提问来源于stack exchange,提问作者VenusianAura

