未找到AppCompatActivity,AndroidStudio运行App崩溃求助(Udacity学员)
Hey there! Sorry to hear you're hitting these frustrating crashes while trying to get your first app running on your OnePlus 5—let's break down what's going on and fix it step by step. The key red flag here is "Didn't find AppCompatActivity on path"—this is almost always a problem with your project's dependencies not being set up correctly.
Step 1: Check your Module-level build.gradle file
The AppCompatActivity class comes from the AndroidX AppCompat library, so we need to make sure it's properly included in your project:
- Open the
build.gradlefile located inside theappfolder of your project (not the top-level projectbuild.gradle—this is a common mix-up!). - Look for the
dependenciesblock, and confirm it includes this line (you can use the latest version or the one recommended in your Udacity course):implementation 'androidx.appcompat:appcompat:1.6.1' - If the line is missing, add it; if it's already there, double-check the version number isn't outdated.
Step 2: Verify your Activity inheritance
Open your main Activity class (probably MainActivity.java or MainActivity.kt) and make sure it extends AppCompatActivity, not the basic Activity class:
- For Java:
public class MainActivity extends AppCompatActivity { // ... your existing code here } - For Kotlin:
class MainActivity : AppCompatActivity() { // ... your existing code here }
Step 3: Sync, Clean, and Rebuild your project
- After updating the
build.gradlefile, click the Sync Now button that pops up in the top-right corner of Android Studio to download the missing dependency. - Once sync finishes, go to the top menu: Build > Clean Project to clear any cached build files that might be causing conflicts.
- Then run Build > Rebuild Project to create a fresh, clean build of your app.
Step 4: Double-check your OnePlus 5 debugging settings
While the core issue is the dependency, let's rule out any device-side hiccups:
- Ensure USB Debugging is enabled in your phone's Developer Options (if you don't see Developer Options, go to Settings > About Phone and tap the Build Number 7 times to unlock it).
- When you connect your phone to your computer, select File Transfer/Android Auto mode, and tap "Allow" on the USB debugging prompt that pops up on your phone screen.
The different crash messages ("MyApp已停止" and "Happy Birthday keeps stopping") are likely just leftover from previous project builds or name changes—once we fix the AppCompatActivity dependency issue, these crashes should disappear entirely.
内容的提问来源于stack exchange,提问作者Oatnuj

