How to handle an ActivityNotFoundException?

In my application, I need to use startActivity to view the contents of a file or use the default application to open a specific file, but sometimes the Android system may not install the desired application.

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

Any advice? thank

+5
source share
4 answers

Just add this activity to the manifest file.

like

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

EDIT:

Now, to catch ActivityNOtFoundException, enter your code,

try {

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

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

. , ActivityNotFound Exception, , , , , .

+11

resolveActivity

    if (intent.resolveActivity(getPackageManager()) != null) {
        startActivity(intent);
    }else {
        Toast.makeText(this,"No suitable app found!",Toast.LENGTH_SHORT).show();
    }
+9

, : " - ". :

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?
}

Warning, do not abuse it: it is dangerous for silent exemptions of exceptions and can make your application unstable and introduce strange and difficult to debug behavior.

+2
source

If you want to display the error as a toast, then

try {
    startActivity(intent);

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

The error occurs due to activity not mentioned in the manifest file.

<activity android:name=".yourActivity"
      android:label="@string/app_name">
</activity>
+1
source

All Articles