简体   繁体   English

错误 android.content.ActivityNotFoundException:仅在极少数设备中

[英]Error android.content.ActivityNotFoundException: only in very few devices

Getting android.content.ActivityNotFoundException: in very few ( example- Android 10, Nokia ) devices on onRequestPermissionsResult but it works perfectly fine in all other devices.onRequestPermissionsResult上获取android.content.ActivityNotFoundException:在极少数(例如 Android 10,诺基亚)设备中,但它在所有其他设备中都可以正常工作。

 @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.当对 Context# startActivity或其变体之一的call失败时,将引发此异常,因为找不到 Activity 来执行给定的 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 .您假设这大约 20 亿台 Android 设备中的每一个都有一个或多个应用程序,其活动与Intent.ACTION_MAINIntent.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.将您的 startActivity() 调用包装在try/catch block中并捕获 ActivityNotFoundException,然后告诉用户您找不到合适的应用程序。

Option #2:

Use PackageManager and queryIntentActivities() to see if there are any matches for the Intent.使用PackageManagerqueryIntentActivities()查看 Intent 是否有任何匹配项。 If there are none, do not call startActivity() , then tell the user that you cannot find a suitable app.如果没有,不要调用startActivity() ,然后告诉用户你找不到合适的应用程序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM