全部产品
云市场

android_content_ActivityNotFoundException

更新时间:2018-04-19 17:27:07

问题分析:

无法找到对应的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路径正确

  1. <activity
  2. android:name="com.your.package.name.YourActivity"
  3. android:label="@string/app_name">
  4. <intent-filter>
  5. <action android:name="android.intent.action.MAIN" />
  6. <category android:name="android.intent.category.LAUNCHER" />
  7. </intent-filter>
  8. </activity>

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

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

参考文献:

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语法)