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

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—the Caused 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 findViewById before setContentView(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_start vs btnStart are not the same!).

Fix:

  • Double-check that every findViewById call comes after setContentView.
  • 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/xxx reference.
  • Make sure those files exist in the res/drawable, res/values/strings.xml, or res/values/styles.xml folders, 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.xml and 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 Project then Build > Rebuild Project in 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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.11 09:15:14