Getting android.content.ActivityNotFoundException:
in very few ( example- Android 10, Nokia ) devices on onRequestPermissionsResult
but it works perfectly fine in all other devices.
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
switch (requestCode) {
case 123:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// Permission Granted
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);
} else {
// Permission Denied
Toast.makeText(Pic.this, "Gallery_Permission Denied", Toast.LENGTH_SHORT)
.show();
}
break;
case 789:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// Permission Granted
camera_getpic();
}
else {
// Permission Denied
Toast.makeText(Pic.this, "Camera_Permission Denied", Toast.LENGTH_SHORT)
.show();
}
break;
case 555:
if ((grantResults.length > 0) && (grantResults[0] == PackageManager.PERMISSION_GRANTED)) {
// Permission Granted
camera_continue();
} else {
// Permission Denied
Toast.makeText(Pic.this, "Permission Denied", Toast.LENGTH_SHORT)
.show();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Error.
java.lang.RuntimeException:
at android.app.ActivityThread.deliverResults (ActivityThread.java:4873)
at android.app.ActivityThread.handleSendResult (ActivityThread.java:4914)
at android.app.servertransaction.ActivityResultItem.execute (ActivityResultItem.java:51)
at android.app.servertransaction.TransactionExecutor.executeCallbacks (TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute (TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:2041)
at android.os.Handler.dispatchMessage (Handler.java:107)
at android.os.Looper.loop (Looper.java:214)
at android.app.ActivityThread.main (ActivityThread.java:7386)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:980)
Caused by: android.content.ActivityNotFoundException:
at android.app.Instrumentation.checkStartActivityResult (Instrumentation.java:2058)
at android.app.Instrumentation.execStartActivity (Instrumentation.java:1716)
at android.app.Activity.startActivityForResult (Activity.java:5192)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:767)
at android.app.Activity.startActivityForResult (Activity.java:5150)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:754)
**at com.sample.pic.Pic.onRequestPermissionsResult (Pic.java:334)**
at android.app.Activity.dispatchRequestPermissionsResult (Activity.java:8264)
at android.app.Activity.dispatchActivityResult (Activity.java:8114)
at android.app.ActivityThread.deliverResults (ActivityThread.java:4866)
Cause :
This exception is thrown when a call
to Context# startActivity
or one of its variants fails because an Activity can not be found to execute the given Intent.
You are assuming that each and every one of those ~2 billion Android devices have one or more apps with an activity matching Intent.ACTION_MAIN
and Intent.CATEGORY_APP_YourAppName
. There is no requirement for any of those devices to have such an activity.
Solution
Option #1:
Wrap your startActivity() call in a try/catch block
and catch the ActivityNotFoundException, then tell the user that you cannot find a suitable app.
Option #2:
Use PackageManager
and queryIntentActivities()
to see if there are any matches for the Intent. If there are none, do not call startActivity()
, then tell the user that you cannot find a suitable app.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.