Приложение вылетает android-toast Kotlin
При нажатии на кнопку должно выводиться уведомление с текстом и картинкой.
Однако при нажатии приложение просто появляется черный экран и приложение вылетает.
Помогите, пожалуйста, разобраться
...
val button: Button = findViewById(R.id.button_notice)
button.setOnClickListener {
val toast = Toast.makeText(applicationContext, R.string.learnkotlin, Toast.LENGTH_LONG)
toast.setGravity(Gravity.BOTTOM, 0, 0)
val toastContainer = toast.view as LinearLayout
val learnImage = ImageView(this)
learnImage.setImageResource(R.drawable.kotlin_logo)
toastContainer.addView(learnImage, 0)
toast.show()
}
...
Вот ошибки из logcat:
2022-03-20 19:16:12.491 12496-12496/com.example.notification_kotlin E/Toast: setGravity() shouldn't be called on text toasts, the values won't be used
2022-03-20 19:16:12.506 12496-12496/com.example.notification_kotlin E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.notification_kotlin, PID: 12496
java.lang.NullPointerException: null cannot be cast to non-null type android.widget.LinearLayout
at com.example.notification_kotlin.MainActivity.onCreate$lambda-0(MainActivity.kt:20)
at com.example.notification_kotlin.MainActivity.$r8$lambda$gh0bxtkEy_cl8jl923-C5Pnh0sg(Unknown Source:0)
at com.example.notification_kotlin.MainActivity$$ExternalSyntheticLambda0.onClick(Unknown Source:2)
at android.view.View.performClick(View.java:7448)
at com.google.android.material.button.MaterialButton.performClick(MaterialButton.java:1131)
at android.view.View.performClickInternal(View.java:7425)
at android.view.View.access$3600(View.java:810)
at android.view.View$PerformClick.run(View.java:28305)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)