7

In my app , I need to use startActivity to see the content of the file , or use the default application to open the certain file , but sometimes the android system may not install the application which is needed .

My question is how to handle this exception . I want a toast , not FC..

Any Advice? THX

xuyao
  • 179
  • 1
  • 2
  • 10

4 Answers4

15

Just simply add that activity in your manifest file..

like,

<activity android:name=".ActivityName"
                  android:label="@string/app_name">
        </activity>

EDIT:

Now to catch the ActivityNOtFoundException put your code in,

try {

  // Your startActivity code wich throws exception  
} catch (ActivityNotFoundException activityNotFound) {

    // Now, You can catch the exception here and do what you want
}

Note: Be careful when you catch this ActivityNotFound Exception but you can't modified manifest file to run time, means once you encountered the exception and if you want to add that this activity tag at runtime then you can't.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Sorry guy.. This is not what I need. I want let the system to decide which activity to run . – xuyao Dec 07 '11 at 03:57
13

Android 11 update:

If you target SDK version 30 or above, you shouldn't use resolveActivity anymore due to new package visibility rules. You'd better simply use try/catch solution mentioned in the accepted answer instead. For more information refer to the CommonsWare's answer

Old answer: (deprecated)

You can use resolveActivity method

    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }else {
        Toast.makeText(this,"No suitable app found!",Toast.LENGTH_SHORT).show();
    }
Ehsan Heidari
  • 486
  • 6
  • 17
  • 1
    be careful with this, I think this now fails on Android 11 (when you target SDK 30+) due to new package visibility rules but these are configurable with permissions: https://developer.android.com/about/versions/11/privacy/package-visibility – hmac Apr 27 '21 at 08:17
2

I think your question is more: "how can I catch a certain exception and prevent a force crash". This is how you do it in code:

try {
    // here is your code that can potentially throw the exception and the force crash
} catch (ActivityNotFoundException activityNotFound) {
    Toast.makeText(this, "your error message", Toast.LENGTH_SHORT).show();
    // maybe also log the exception, for future debugging?
}

A warning, don't abuse this: it's dangerous to "silently swallow" exceptions and can make your application unstable and introduce weird and hard-to-debug behaviour.

Guillaume
  • 22,694
  • 14
  • 56
  • 70
1

If you want to display error as toast then

try {
    startActivity(intent);

} catch (ActivityNotFoundException e) {
    // TODO: handle exception
    //Show Toast...
}

The error occurs because the activity not mentioned in the manifest file.

<activity android:name=".yourActivity"
      android:label="@string/app_name">
</activity>
Pratik
  • 30,639
  • 18
  • 84
  • 159
droid kid
  • 7,569
  • 2
  • 32
  • 37