0

I have a problem with my BroadcastReceiver class that it is inside my MainActivity as a inner class. Every time that I click on a button in my app's notification, I get this error:

java.lang.RuntimeException: Unable to instantiate receiver com.oniktech.testmediaservice.MainActivity$MediaReceiver: java.lang.InstantiationException: java.lang.Class<com.oniktech.testmediaservice.MainActivity$MediaReceiver> has no zero argument constructor

here is my code:

val playPauseAction = NotificationCompat.Action(
            icon, play_pause,
            MediaButtonReceiver.buildMediaButtonPendingIntent(
                this,
                PlaybackStateCompat.ACTION_PLAY_PAUSE
            )
        )

    builder.setContentTitle("my test")
                .addAction(playPauseAction)
                .setStyle(
                    androidx.media.app.NotificationCompat.MediaStyle()
                        .setMediaSession(mediaSession.getSessionToken())
                        .setShowActionsInCompactView(0)
                )

            notificatioManager = getSystemService(NOTIFICATION_SERVICE) as NotificationManager
            notificatioManager.notify(0, builder.build())

and here is my inner class:

inner class MediaReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            MediaButtonReceiver.handleIntent(mediaSession, intent)
        }
    }

I know that I should not use inner class in this case. But I have to use my 'mediaSession' object in it. What should I do? thanks for your help.

Ehsan Nokandi
  • 1,933
  • 2
  • 10
  • 14
  • Must be static or non-static but registered inside parent class: https://stackoverflow.com/questions/29947038/java-lang-instantiationexception-class-has-no-zero-argument-constructor – Jarvis Oct 30 '19 at 17:02
  • Remove inner and it should be static by default. – Jarvis Oct 30 '19 at 17:04
  • @Jarvis yes but how can I do than in Kotlin. – Ehsan Nokandi Oct 30 '19 at 17:11
  • Remove the inner keyword from class MediaReceiver. – Jarvis Oct 30 '19 at 17:13
  • @Jarvis Then I can't use my mediaSession object inside my MediaReceiver class. I define my mediaSession inside MainActivity and I need to use it here. I'm really confused. – Ehsan Nokandi Oct 30 '19 at 17:18
  • @EhsanNokandi you should not add any dependency from your activity to your receiver, because the receiver will be instantiated by the system. So if you want your `mediaSession` you should have some mechanism to create it inside your receiver. – momvart Oct 30 '19 at 18:56
  • @MohammadOmidvar Thanks dear Mohammad. I think you are right. Can you do me a favor and give me your email address? I have a question about MediaService and I have no one to ask. I would appreciate if you helped me with this. – Ehsan Nokandi Oct 30 '19 at 20:41

1 Answers1

2

Problem - 1: Your MediaReceiver should be Nested only not inner. In Kotlin Nested class is static by default. So remove inner keyword.

class MediaReceiver : BroadcastReceiver() {
}

Problem - 2: You want to access outer class property from BroadcastReceiver which is not good practice. You should avoid it. But if you still want to do so you should make that property static and use it inside Receiver. Check my below sample implementation.

class MainActivity {
    companion object {
        var mediaSession: MediaSessionCompat? = null
    }

    class MediaReceiver : BroadcastReceiver() {
        override fun onReceive(context: Context, intent: Intent) {
            MediaButtonReceiver.handleIntent(mediaSession, intent)
        }
    }
}
Md. Asaduzzaman
  • 14,963
  • 2
  • 34
  • 46