【问题标题】:App works in emulator but crashes on a real device应用程序在模拟器中运行,但在真实设备上崩溃
【发布时间】:2022-10-02 10:05:22
【问题描述】:

在模拟器中,我的应用程序运行良好,但是当我在真正的三星 Galaxy 设备上使用该应用程序时,该应用程序将崩溃。我现在已经调试它并看到这个错误:

2022-04-02 22:41:18.584 16611-16611/? E/USNET: USNET: appName: ch.yourclick.ki 2022-04-02 22:41:19.546 16611-16611/ch.yourclick.ki E/AndroidRuntime: 致命例外: 主进程: ch.yourclick.ki, PID: 16611 java.lang.RuntimeException:无法启动服务 ch.yourclick.ki.services.PicovoiceService@d2ab860 与 Intent { cmp=ch.yourclick.ki/.services.PicovoiceService(有附加功能)}: java.lang.IllegalArgumentException: ch.yourclick.ki: 目标 S+ (版本 31 及更高版本)要求 FLAG_IMMUTABLE 或 创建 PendingIntent 时指定 FLAG_MUTABLE。强烈 考虑使用 FLAG_IMMUTABLE,仅在某些情况下使用 FLAG_MUTABLE 功能取决于 PendingIntent 是可变的,例如如果它 需要与内联回复或气泡一起使用。在 android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5110) 在 android.app.ActivityThread.access$2200(ActivityThread.java:310) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2319) 在 android.os.Handler.dispatchMessage(Handler.java:106) 在 android.os.Looper.loopOnce(Looper.java:226) 在 android.os.Looper.loop(Looper.java:313) 在 android.app.ActivityThread.main(ActivityThread.java:8663) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) 引起:java.lang.IllegalArgumentException:ch.yourclick.ki: 定位 S+(版本 31 及更高版本)需要满足以下条件之一 FLAG_IMMUTABLE 或 FLAG_MUTABLE 在创建时指定 待定意图。强烈考虑使用FLAG_IMMUTABLE,只使用 FLAG_MUTABLE 如果某些功能依赖于 PendingIntent 是 可变的,例如如果它需要与内联回复或气泡一起使用。 在 android.app.PendingIntent.checkFlags(PendingIntent.java:382) 在 android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465) 在 android.app.PendingIntent.getActivity(PendingIntent.java:451) 在 android.app.PendingIntent.getActivity(PendingIntent.java:415) 在 ch.yourclick.ki.services.PicovoiceService.getNotification(PicovoiceService.java:142) 在 ch.yourclick.ki.services.PicovoiceService.onStartCommand(PicovoiceService.java:127) 在 android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5092) ... 9 更多

这就是我启动服务的方式:

mDialog.startService(getContext()); // <-- App only crashes when I use this line
public void startService(Context context) {
    mServiceIntent = new Intent(context, PicovoiceService.class);
    mServiceIntent.putExtra(\"keywordFileName\", \"Hey-Ki_de_android_v2_1_0.ppn\");
    mServiceIntent.putExtra(\"contextFileName\", \"Ki_de_android_v2_1_0.rhn\");
    ContextCompat.startForegroundService(context, mServiceIntent);
}

在我的应用程序中一切正常,所以只有当我按下按钮启动服务PicovoiceService.class 时应用程序才会崩溃。

我的代码有什么问题,为什么它可以在模拟器中运行,但不能在真实设备上运行?

  • 您的模拟器正在运行什么 API 级别?
  • @Flash103 它是 API 30。

标签: java android


【解决方案1】:

根据堆栈,这似乎是您崩溃的相关原因: java.lang.IllegalArgumentException: ch.yourclick.ki: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a PendingIntent. Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable

关于您关于为什么它在模拟器中而不是在您的物理设备上工作的问题,您的模拟器正在运行 API 30,这低于要求。如果您使用 API 31 运行模拟器,您会像在物理设备上一样遇到这种崩溃。

【讨论】:

    【解决方案2】:

    我可以通过将此依赖项添加到 greadle 文件来使其工作:

    implementation 'androidx.work:work-runtime-ktx:2.7.1'
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-01-17
      • 2015-07-26
      • 1970-01-01
      • 2013-11-24
      • 2022-01-22
      • 2021-04-16
      相关资源
      最近更新 更多