简体   繁体   English

来自 Play 管理中心的 android.content.ActivityNotFoundException 崩溃报告

[英]android.content.ActivityNotFoundException crash report from Play Console

Stacktrace:堆栈跟踪:

android.content.ActivityNotFoundException: 
  at android.app.Instrumentation.checkStartActivityResult (Instrumentation.java:1878)
  at android.app.Instrumentation.execStartActivity (Instrumentation.java:1545)
  at android.app.Activity.startActivityForResult (Activity.java:4283)
  at androidx.activity.ComponentActivity.startActivityForResult (ComponentActivity.java)
  at androidx.core.app.ActivityCompat.startActivityForResult (ActivityCompat.java)
  at androidx.activity.ComponentActivity$2.onLaunch (ComponentActivity.java)
  at androidx.activity.result.ActivityResultRegistry$2.launch (ActivityResultRegistry.java)
  at androidx.fragment.app.Fragment$9.launch (Fragment.java)
  at androidx.activity.result.ActivityResultLauncher.launch (ActivityResultLauncher.java)
  at cyberdynesoftware.jahresurlaubapp.YearFragment.lambda$showSchoolVacationDialog$5 (YearFragment.java)
  at cyberdynesoftware.jahresurlaubapp.YearFragment.lambda$showSchoolVacationDialog$5$YearFragment (YearFragment.java)
  at cyberdynesoftware.jahresurlaubapp.-$$Lambda$YearFragment$FLinyacBVg2yJZUeB9ggaBhktlA.onClick (lambda)
  at com.android.internal.app.AlertController$ButtonHandler.handleMessage (AlertController.java:173)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:158)
  at android.app.ActivityThread.main (ActivityThread.java:7224)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1230)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1120)

Unfortunately I couldn't reproduce the error locally.不幸的是,我无法在本地重现该错误。 The stacktrace stems from Play Console;堆栈跟踪源于 Play Console; it happend on a Samsung Galaxy S5.它发生在三星 Galaxy S5 上。 This is the relevant part of the code in YearFragment:这是 YearFragment 中代码的相关部分:

private void showSchoolVacationDialog() {
    new AlertDialog.Builder(requireContext())
            .setTitle(R.string.schoolVacation)
            .setMessage(R.string.schoolVacationDialogMessage)
            .setPositiveButton(R.string.choose, (dialog, which) -> {
                activityResult.launch("text/calendar");
                dialog.dismiss();
            })
            .setNegativeButton(R.string.clear, (dialog, which) -> {
                db.schoolVacationDao().deleteAll();
                adapter.notifyDataSetChanged();
                dialog.dismiss();
            })
            .create()
            .show();
}

private final ActivityResultLauncher<String> activityResult = registerForActivityResult(
        new ActivityResultContracts.GetContent(),
        result -> {
            if (result != null)
                try {
                    InputStream inputStream = requireContext().getContentResolver().openInputStream(result);
                    if (inputStream != null) {
                        Scanner scanner = new Scanner(inputStream).useDelimiter("\\A");
                        addSchoolVacationToDB(iCalParser.parse(scanner.next()));
                        scanner.close();
                        inputStream.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
        });

Why does this happen?为什么会这样? Is there anything fishy with my code?我的代码有什么可疑之处吗?

As per the documentation on launch() :根据有关launch()的文档

This method throws ActivityNotFoundException if there was no Activity found to run the given Intent.如果没有找到运行给定 Intent 的 Activity,此方法将抛出ActivityNotFoundException

While any of the ActivityResultContracts (such as the GetContent one you're using) should be available on every device, users may be running a custom build of Android that removes the apps / system utilities that handle these common intents or the user may have manually disabled the app (this is more common with things like a Browser or Camera app than this particular case).虽然任何ActivityResultContracts (例如您正在使用的GetContent都应该在每台设备上可用,但用户可能正在运行 Android 的自定义构建,该构建删除了处理这些常见意图的应用程序/系统实用程序,或者用户可能手动拥有禁用该应用程序(这在浏览器或相机应用程序中比这种特殊情况更常见)。

Therefore you should consider surrounding your call to launch() with a try / catch block that catches an ActivityNotFoundException and informs the user that their device does not support this functionality.因此,您应该考虑使用捕获ActivityNotFoundException并通知用户他们的设备不支持此功能的try / catch块围绕您对launch()的调用。

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

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