无法启动 activity:java.lang.IllegalArgumentException:针对 S+(版本 31 及更高版本)需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一

Unable to start activity: java.lang.IllegalArgumentException: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE

今天我的 phone 更新为 android 12,我的应用程序在启动时开始崩溃。它应该在启动时发送通知。

val intent = Intent(this, MainActivity::class.java)
        val pendingIntent: PendingIntent? = TaskStackBuilder.create(this).run {
            addNextIntentWithParentStack(intent)
            getPendingIntent(INTENT_REQUEST, PendingIntent.FLAG_IMMUTABLE) //Used to be: FLAG_UPDATE_CURRENT
        }

        //Configurar llamada desde boton de notificacion
        val buttonIntent = Intent(this, LlamadaNotificacion::class.java)
        buttonIntent.putExtra("LLAMADA", "LLAMAR")

        val buttonPendingIntent: PendingIntent =
            PendingIntent.getActivity(this, BUTTON_INTENT_REQUEST, buttonIntent, PendingIntent.FLAG_ONE_SHOT)

        val boton = NotificationCompat.Action.Builder(
            R.drawable.dry,"Presiona para realizar llamada de emergencia", buttonPendingIntent
        ).build()

        val notification = NotificationCompat.Builder(this,channelID).also {
            it.setContentTitle("Daira Mayari Mendez Gutierrez")
            it.setContentText("Presiona para más información")
            it.setSmallIcon(R.mipmap.ic_logo)
            //it.setPriority(NotificationCompat.PRIORITY_HIGH)
            it.setContentIntent(pendingIntent)
            it.setVisibility(VISIBILITY_PUBLIC)
            it.addAction(boton)
            it.setAutoCancel(false)
        }.build()

        val notificationManager = NotificationManagerCompat.from(this)

        notificationManager.notify(notificationId,notification)

我已经尝试使用可变和不可变标志,但调试器在“PendingIntent.getActivity”行中说了一些错误,如下所示。

这是我妹妹的急救应用程序,非常感谢您的帮助。谢谢。

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.informacion, PID: 11671
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.informacion/com.example.informacion.MainActivity}: java.lang.IllegalArgumentException: com.example.informacion: 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, e.g. if it needs to be used with inline replies or bubbles.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4037)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:4203)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103)
        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:2440)
        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:8641)
        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:1133)
     Caused by: java.lang.IllegalArgumentException: com.example.informacion: 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, e.g. if it needs to be used with inline replies or bubbles.
        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 com.example.informacion.MainActivity.onCreate(MainActivity.kt:88)
        at android.app.Activity.performCreate(Activity.java:8282)
        at android.app.Activity.performCreate(Activity.java:8262)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:4011)
            ... 12 more

你这样做:

    val buttonPendingIntent: PendingIntent =
        PendingIntent.getActivity(this, BUTTON_INTENT_REQUEST, buttonIntent, PendingIntent.FLAG_ONE_SHOT)

您还必须将 FLAG_IMMUTABLEFLAG_MUTABLE 添加到此。