You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android开发:res下raw文件夹存在但R.raw.Gravity代码报红求助

Troubleshooting R.raw.Gravity Red Error in Android

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 to gravity.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:

    1. Go to Build > Clean Project to clear outdated build files.
    2. Run Build > Rebuild Project to regenerate the R class from scratch.
    3. You can also click the "Sync Project with Gradle Files" button (the elephant icon in the top toolbar) to force a sync.
  • 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 present
    
  • Fix 4: Verify the raw folder's location
    Double-check that your raw folder is in the correct path: src/main/res/raw. If it's in the wrong directory (like inside src/main/java) or misspelled (e.g., raws), the resource won't be detected.

  • Fix 5: Check for a corrupted audio file
    If the Gravity file 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 19:27:32