简体   繁体   English

应用程序在模拟器中运行但在真实设备上崩溃

[英]App works in emulator but crashes on a real device

In emulator, my app works perfectly fine but when I use the app on my real Samsung Galaxy device, the app will crash.在模拟器中,我的应用程序运行良好,但当我在我真正的三星 Galaxy 设备上使用该应用程序时,该应用程序会崩溃。 I have now debugged it and see this error:我现在调试它并看到这个错误:

2022-04-02 22:41:18.584 16611-16611/? 2022-04-02 22:41:18.584 16611-16611/? E/U.NET: U.NET: appName: ch.yourclick.ki 2022-04-02 22:41:19.546 16611-16611/ch.yourclick.ki E/AndroidRuntime: FATAL EXCEPTION: main Process: ch.yourclick.ki, PID: 16611 java.lang.RuntimeException: Unable to start service ch.yourclick.ki.services.PicovoiceService@d2ab860 with Intent { cmp=ch.yourclick.ki/.services.PicovoiceService (has extras) }: 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. E/U.NET:U.NET: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 with Intent { cmp=ch.yourclick.ki/.services.PicovoiceService (有额外) }: java.lang .IllegalArgumentException: ch.yourclick.ki: Targeting S+(版本 31 及以上)要求在创建 PendingIntent 时指定 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一。 Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, eg if it needs to be used with inline replies or bubbles.强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于可变的 PendingIntent 时才使用 FLAG_MUTABLE,例如,如果它需要与内联回复或气泡一起使用。 at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5110) at android.app.ActivityThread.access$2200(ActivityThread.java:310) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2319) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread.main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1135) Caused by: 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 Pend at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5110) at android.app.ActivityThread.access$2200(ActivityThread.java:310) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2319) at android .os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:226) at android.os.Looper.loop(Looper.java:313) at android.app.ActivityThread. main(ActivityThread.java:8663) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:567) at com.android.internal.os.ZygoteInit. main(ZygoteInit.java:1135) Caused by: java.lang.IllegalArgumentException: ch.yourclick.ki: Targeting S+ (version 31 and above) requires one of FLAG_IMMUTABLE or FLAG_MUTABLE be specified when creating a Pend 原因: ingIntent.意图。 Strongly consider using FLAG_IMMUTABLE, only use FLAG_MUTABLE if some functionality depends on the PendingIntent being mutable, eg if it needs to be used with inline replies or bubbles.强烈考虑使用 FLAG_IMMUTABLE,仅当某些功能依赖于可变的 PendingIntent 时才使用 FLAG_MUTABLE,例如,如果它需要与内联回复或气泡一起使用。 at android.app.PendingIntent.checkFlags(PendingIntent.java:382) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465) at android.app.PendingIntent.getActivity(PendingIntent.java:451) at android.app.PendingIntent.getActivity(PendingIntent.java:415) at ch.yourclick.ki.services.PicovoiceService.getNotification(PicovoiceService.java:142) at ch.yourclick.ki.services.PicovoiceService.onStartCommand(PicovoiceService.java:127) at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:5092)... 9 more at android.app.PendingIntent.checkFlags(PendingIntent.java:382) at android.app.PendingIntent.getActivityAsUser(PendingIntent.java:465) at android.app.PendingIntent.getActivity(PendingIntent.java:451) at android.app. PendingIntent.getActivity(PendingIntent.java:415)在 ch.yourclick.ki.services.PicovoiceService.getNotification(PicovoiceService.java:142)在 ch.yourclick.ki.services.PicovoiceService.onStartCommand(PicovoiceService.java:882132469415888): .app.ActivityThread.handleServiceArgs(ActivityThread.java:5092)... 9 更多

This is how I start the service:这就是我启动服务的方式:

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);
}

In my app everything works fine, so the app only crashes when I press on a button to start the service PicovoiceService.class .在我的应用程序中,一切正常,所以只有当我按下按钮启动服务PicovoiceService.class时,应用程序才会崩溃。

What is wrong with my code and why does it work in the emulator but not on a real device?我的代码有什么问题,为什么它可以在模拟器中运行,但不能在真实设备上运行?

According to the stack, this seems like the relevant cause of your crash: 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根据堆栈,这似乎是导致崩溃的相关原因: 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 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

Regarding your question as to why it works in the emulator and not on your physical device, your emulator is running API 30, which is below the requirement.关于为什么它在模拟器中工作而不在您的物理设备上工作的问题,您的模拟器正在运行 API 30,这低于要求。 If you ran an emulator with API 31, you would experience this crash just as you do on the physical device.如果您使用 API 31 运行模拟器,您会像在物理设备上一样遇到此崩溃。

I could make it work by adding this dependency to the greadle file:我可以通过将此依赖项添加到 greadle 文件来使其工作:

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

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

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