【问题标题】:Unable to start activity: java.lang.IllegalArgumentException: Targeting S+ (version 31 and above) requires that one of FLAG_IMMUTABLE or FLAG_MUTABLE无法启动活动:java.lang.IllegalArgumentException:定位 S+(版本 31 及更高版本)需要 FLAG_IMMUTABLE 或 FLAG_MUTABLE 之一
【发布时间】:2022-07-22 14:59:13
【问题描述】:

今天我的手机更新到 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

【问题讨论】:

标签: android-studio android-intent android-notifications android-pendingintent android-12


【解决方案1】:

你这样做:

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

您还必须将FLAG_IMMUTABLEFLAG_MUTABLE 添加到此。

【讨论】:

  • 工作完美,谢谢!!!!
【解决方案2】:

你需要做的是: 放入你的 build.gradle

def work_version = "2.7.1"
    implementation "androidx.work:work-runtime:$work_version"
    // optional - Test helpers
    androidTestImplementation "androidx.work:work-testing:$work_version"

然后

去编辑>查找>在文件中查找并搜索PendingIntent,如果你使用java代码放|PendingIntent.FLAG_IMMUTABLE如果Kotlin然后或PendingIntent.FLAG_IMMUTABLE

您的问题将得到 100% 解决: 加油!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-08-22
    • 2022-07-09
    • 1970-01-01
    • 2022-10-05
    • 2021-11-03
    • 2022-01-08
    • 2021-09-29
    • 1970-01-01
    相关资源
    最近更新 更多