Android Studio模拟器运行应用崩溃,新手开发者求助
Hey there! As someone who’s stared at that frustrating "app keeps stopping" message way too many times when I was starting out, let’s break this down—even if your code looks identical to the tutorial, there are tiny, easy-to-miss things that can cause crashes. Here’s what to check first, along with example code fixes:
Don’t just rely on the emulator’s generic error pop-up. Open the Logcat panel at the bottom of Android Studio, filter for Error level, and look for the stack trace. It’ll tell you exactly what’s breaking—like a NullPointerException or ActivityNotFoundException. This is non-negotiable for debugging crashes.
Even if your code matches the tutorial, these small details can trip you up:
Layout ID Mismatch
It’s easy to mistype an ID in your layout XML or code, even if it looks right at a glance.
Broken Code Example:
// MainActivity.kt - trying to reference an ID that doesn't exist in the layout val greetingText = findViewById<TextView>(R.id.tv_greeting) greetingText.text = "Hello Android!"
<!-- activity_main.xml - actual ID is tv_hello, not tv_greeting --> <TextView android:id="@+id/tv_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Fix:
Make sure the ID in your XML matches exactly what’s in your code:
val greetingText = findViewById<TextView>(R.id.tv_hello) greetingText.text = "Hello Android!"
Missing Activity Registration in Manifest
If you added a new Activity but forgot to register it in AndroidManifest.xml, the system won’t find it when trying to launch.
Broken Manifest Example:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"> <!-- Oops! No <activity> tag for MainActivity --> </application>
Fix:
Add the Activity registration, including the launcher intent filter if it’s your main screen:
<application android:allowBackup="true" android:icon="@mipmap/ic_launcher"> <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> </application>
SDK Version Incompatibility
If the tutorial uses a different SDK version than your project, some API calls might break. For example, using a newer API without checking the Android version:
Broken Code Example (minSdkVersion < 30):
// This will crash on devices running Android 10 or lower val insetsController = ViewCompat.getWindowInsetsController(window.decorView) insetsController?.hide(WindowInsetsCompat.Type.statusBars())
Fix:
Add a version check to support older devices:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { // For Android 11+ val insetsController = window.insetsController insetsController?.hide(WindowInsets.Type.statusBars()) } else { // For older versions (deprecated but still works) @Suppress("DEPRECATION") window.setFlags( WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN ) }
Sometimes the problem isn’t your code at all:
- Try restarting your emulator, or create a brand new emulator device (old images can get corrupted)
- Clear Android Studio’s cache: Go to
File > Invalidate Caches... > Invalidate and Restart
If you share the exact stack trace from Logcat, we can narrow it down even more!
内容的提问来源于stack exchange,提问作者Adnan Abou-Elenein

