Android Studio测验应用无编译报错但启动崩溃求助
Hey there! Don’t stress—this is super common for your first Android project, and we’ll figure out why your quiz app is crashing on the emulator even though it compiles just fine. Compile success only means your code doesn’t have syntax errors; runtime crashes usually come from things like missing resources, uninitialized objects, or misconfigured components. Let’s break this down step by step.
First: Find the Exact Crash Cause with Logcat
Before diving into code, let’s get the root cause from Logcat—it’s your best friend for debugging crashes:
- Open the Logcat window at the bottom of Android Studio.
- Set the log level to
Error(top dropdown menu) and filter by your app’s package name (so you don’t get flooded with system logs). - Look for the line starting with
FATAL EXCEPTION—theCaused by:section right below it will tell you exactly what’s breaking things.
Common Crash Causes & Fixes for Your Quiz App
1. NullPointerException (Most Likely for Newbies)
This happens when you try to use an object that hasn’t been initialized. For example:
- You called
findViewByIdbeforesetContentView(R.layout.activity_main)(so the app can’t find your UI elements yet). - The ID in your Java code doesn’t match the ID in your XML layout (check for typos—
btn_startvsbtnStartare not the same!).
Fix:
- Double-check that every
findViewByIdcall comes aftersetContentView. - Verify that the ID in code (like
findViewById(R.id.btn_quiz_start)) matches exactly what’s in your XML (android:id="@+id/btn_quiz_start").
2. Missing Resource Error
If your XML references a drawable, string, or style that doesn’t exist (or has a typo), the app might compile but crash when trying to load that resource.
Fix:
- Go through your XML layout and check every
@drawable/xxx,@string/xxx, or@style/xxxreference. - Make sure those files exist in the
res/drawable,res/values/strings.xml, orres/values/styles.xmlfolders, and that their names are spelled correctly.
3. Activity Not Registered in Manifest
Even though Android Studio usually does this automatically, if you edited AndroidManifest.xml manually, you might have accidentally removed your MainActivity’s registration.
Fix:
- Open
AndroidManifest.xmland confirm you have this block of code for your MainActivity:
<activity android:name=".MainActivity" android:exported="true"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>
4. Emulator Cache Glitch
Sometimes the crash isn’t your code’s fault—it’s just the emulator being finicky.
Fix:
- Restart the emulator: Click the power button on the emulator toolbar, then restart it.
- Clear app cache: On the emulator, go to Settings > Apps > [Your Quiz App] > Storage > Clear Cache.
- Rebuild your project: Click
Build > Clean ProjectthenBuild > Rebuild Projectin Android Studio.
Quick Debugging Tip for Newbies
Try adding breakpoints in your onCreate method: Click the gutter next to the line numbers in MainActivity, then run the app in debug mode (the green bug icon). The app will pause at each breakpoint, so you can step through code line by line to see exactly where it crashes.
内容的提问来源于stack exchange,提问作者Wami ikechukwu

