文档

android_content_ActivityNotFoundException

更新时间:

问题分析

无法找到对应的Activity时抛出此类异常。

解决方案

无法找到对应的Activity时抛出此类异常,一般发生在AndroidManifest.xml文件没有配置需要的Activity路径所导致的或手机中确实无此应用,建议检查AndroidManifest文件Activity路径并在启动Activity处捕获异常。

代码示例

android.content.ActivityNotFoundException: Unable to find explicit activity class {*}; have you declared this activity in your AndroidManifest.xml?

方案1:确认AndroidManifest.xml中所需Activity路径正确

    <activity
        android:name="com.your.package.name.YourActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

方案2:针对可能不存在的Activity抛出的异常进行捕获

public static void startDialer(Context context, String phoneNumber) {
    try {
        Intent dial = new Intent();
        dial.setAction(Intent.ACTION_DIAL);
        dial.setData(Uri.parse("tel:" + phoneNumber));
        context.startActivity(dial);
    } catch (ActivityNotFoundException ex) {
        Log.e(TAG, "Error starting phone dialer intent.", ex);
        Toast.makeText(context, "Sorry, we couldn't find any app to place a phone call!",
                Toast.LENGTH_SHORT).show();
    }
}

参考文献

1.https://developer.android.com/reference/android/content/ActivityNotFoundException.html

2.http://stackoverflow.com/questions/15825081/error-default-activity-not-found

3.http://www.cnblogs.com/Cjch/archive/2013/05/28/3104561.html

4.https://developer.android.com/guide/topics/manifest/manifest-intro.html (应用清单文件)

5.https://developer.android.com/guide/topics/manifest/activity-element.html (activity语法)

  • 本页导读 (0)
文档反馈