Navigation Drawer布局预览不显示,Android Studio卡在“等待构建完成”
Hey there, let's work through this annoying issue with your Navigation Drawer XML—stuck on "Waiting for build to finish..." and missing layout preview can be such a roadblock. I’ve dealt with similar glitches before, so here are some practical fixes to try:
Force Sync & Clean/Rebuild Your Project
Build cache corruption is a common culprit here. Follow these steps:- Go to
File > Sync Project with Gradle Filesand wait for the sync to complete. - Then run
Build > Clean Project, followed byBuild > Rebuild Project. - Restart Android Studio—this often clears up stuck build processes and preview issues.
- Go to
Verify Your XML File’s Completeness
The code snippet you shared cuts off atxmlns..., so make sure youractivity_main_drawer.xmlhas no syntax errors or incomplete tags. A standard Navigation Drawer menu structure looks like this:<?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <group android:checkableBehavior="single"> <item android:id="@+id/nav_home" android:icon="@drawable/ic_home" android:title="Home" /> <item android:id="@+id/nav_profile" android:icon="@drawable/ic_profile" android:title="Profile" /> </group> <item android:title="Settings"> <menu> <item android:id="@+id/nav_settings" android:icon="@drawable/ic_settings" android:title="App Settings" /> </menu> </item> </menu>Double-check for unclosed tags, misspelled attributes, or missing namespace declarations—these can halt the build and prevent the preview from loading.
Adjust Android Studio Preview Settings
- If the Preview pane isn’t visible, enable it via
View > Tool Windows > Preview. - Make sure the selected API level in the preview dropdown matches a version your project supports (don’t pick one higher than your
compileSdkVersion). - Click the Refresh button at the top of the Preview pane to force a reload.
- If the Preview pane isn’t visible, enable it via
Disable Instant Run
Instant Run can sometimes interfere with build processes:
Navigate toFile > Settings > Build, Execution, Deployment > Instant Run, uncheckEnable Instant Run to hot swap code/resource changes on deploy, then restart Android Studio.Validate Gradle Dependencies
Ensure your module-levelbuild.gradlehas the correct Navigation component dependencies (match versions to your project’s AndroidX setup):dependencies { implementation "androidx.navigation:navigation-fragment-ktx:2.7.6" implementation "androidx.navigation:navigation-ui-ktx:2.7.6" }Sync Gradle after updating these, then try building again.
If none of the above works, try invalidating Android Studio’s caches: Go to File > Invalidate Caches..., check Invalidate and Restart, and let the IDE reload. This fixes many stubborn build/preview issues caused by corrupted cache files.
内容的提问来源于stack exchange,提问作者Dr Mido

