布局XML资源存在但编译器报‘not found’及声音合成器崩溃问题求助
Hey Jim, let's break down your two issues with practical fixes and debugging steps:
Issue 1: Layout XML Resource "Not Found" Error During Compilation
This is a tricky but super common problem—here’s what to check systematically:
- Double-check resource naming & syntax: Android is case-sensitive! Make sure your resource name (e.g.,
@drawable/my_icon) matches the actual file name exactly (no uppercase letters where there shouldn’t be, no typos). Also confirm you’re using the right resource type prefix (@drawable/,@string/,@color/—mixing these up is easy!). - Verify resource directory structure: Ensure your resource is in the correct folder (e.g., a vector drawable might be in
drawable-v24but your layout is referencing it without version qualifiers, causing older SDKs to miss it). Also check if the resource was accidentally placed in adebugorrelease-only folder when it should be in the main resources. - Clean build & invalidate caches: Gradle can sometimes cache old resource references. Run
./gradlew cleanfrom the terminal, then go to Android Studio’sFile > Invalidate Caches... > Invalidate and Restartto clear stale data. - Check for conflicting resources: If you’re using third-party libraries, there might be a resource name collision. Try renaming your local resource to something unique and update the layout reference.
- Fix incorrect R class imports: Accidentally importing
android.Rinstead of your app’s package-specificRclass will cause this error. Double-check your layout’s associated activity/fragment imports.
Issue 2: Audio Synthesizer Crashes on Physical Devices (5.0/7.0) After 8 Runs
The AudioFlinger/AudioTrack error with code -20 gives us a strong clue—here’s how to debug this:
- First, understand error code -20: On Android 5.0 and 7.0, this typically points to resource exhaustion or an invalid operation with
AudioTrack. The 8-run pattern suggests you’re not properly releasing audio resources after each use. - Audit
AudioTracklifecycle: Every time you create anAudioTrackinstance, you must callrelease()when you’re done with it. If you skip this, each run will leak a bit of audio system memory, and by the 8th run, the device hits its resource limit (simulators usually have looser resource constraints than physical phones, which is why it works there). - Capture full crash logs: The partial error info isn’t enough to pinpoint the exact issue. Connect your phone to your computer, run
adb logcat *:Ein the terminal, and reproduce the crash. This will give you the full stack trace—look for lines that show where theAudioTrackoperation is failing (e.g.,initialize()orplay()). - Check thread safety:
AudioTrackoperations shouldn’t run on the main thread. Make sure you’re handling audio playback in a dedicated background thread or usingHandlerThreadfor audio tasks—blocking the main thread can cause unexpected state issues in the audio system. - Regarding API level changes: Android Studio absolutely supports setting
compileSdkVersionandtargetSdkVersionto 21—you might have been looking in the wrong place. Check your app-levelbuild.gradlefile (not the project-level one) and update those values manually if the UI dropdown isn’t showing it. That said, the crash is unlikely to be caused by the API level itself—focus on resource cleanup first.
内容的提问来源于stack exchange,提问作者Jim Tan
相关产品推荐
相关产品推荐

