How to Fix Android ActivityNotFoundException
In this tutorial, you'll learn about How to Fix Android ActivityNotFoundException. We cover key concepts, practical examples, and best practices to help you understand and apply this topic effectively.
The Problem
You start an activity and the app crashes:
android.content.ActivityNotFoundException:
Unable to find explicit activity class {com.example/com.example.SecondActivity}
Or:
No Activity found to handle Intent { act=android.intent.action.VIEW }
Android cannot find the target activity because it is not declared in the manifest, the class name is wrong, or the intent action has no matching handler.
Quick Fix
Step 1: Register the activity in AndroidManifest.xml
<application ...>
<activity
android:name=".SecondActivity"
android:exported="false" />
</application>
Every activity used in the app must have an <activity> element inside <application>. For exported activities (launchable from other apps), set Android:exported="true".
Step 2: Verify the class name in the intent
// Wrong - string literal class name
val intent = Intent(this, "com.example.SecondActivity")
// Right - use the class reference
val intent = Intent(this, SecondActivity::class.java)
startActivity(intent)
Using ::class.java avoids typos and keeps the reference type-safe.
Step 3: Check the package name
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp">
The activity name .SecondActivity resolves to com.example.myapp.SecondActivity. If you use a fully qualified name, it must match the package in the manifest.
Step 4: Handle implicit intents correctly
// Verify an app can handle the intent before launching
val intent = Intent(Intent.ACTION_VIEW, uri)
if (intent.resolveActivity(packageManager) != null) {
startActivity(intent)
}
Always check resolveActivity before calling startActivity for implicit intents.
Step 5: Add intent filter for exported activities
<activity android:name=".ShareActivity" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>
Without an <intent-filter>, third-party apps cannot resolve your activity.
Prevention
- Register every activity in the manifest immediately when creating it.
- Use
::class.javareferences instead of string class names. - Add
resolveActivitychecks before all implicit intents.
Common Mistakes with activity not found
- Using
foldlinstead offoldl'causing stack overflow on large lists - Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
These mistakes appear frequently in real-world Android code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
DodaTech Tool Reference
Use Doda Browser's App Inspector to view all registered activities in installed APKs on your device. This helps verify that the manifest is packaged correctly.
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro