Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android crash on boot - MediaButtonReceiver may not be null

I'm having an issue in my Unity app where the Android build crashes on boot on a device running Android 4.3 The app loads a native plugin I wrote for playing music in the background while the app is suspended. It's using the Android MediaBrowser APIs and as such I'm using the "Compat" versions so continue support for previous versions of Android.

06-07 09:59:37.000: E/AndroidRuntime(1748): FATAL EXCEPTION: main
06-07 09:59:37.000: E/AndroidRuntime(1748): java.lang.Error: FATAL EXCEPTION [main]
06-07 09:59:37.000: E/AndroidRuntime(1748): Unity version     : 5.6.1f1
06-07 09:59:37.000: E/AndroidRuntime(1748): Device model      : samsung GT-I9300
06-07 09:59:37.000: E/AndroidRuntime(1748): Device fingerprint: samsung/m0xx/m0:4.3/JSS15J/I9300XXUGMK6:user/release-keys
06-07 09:59:37.000: E/AndroidRuntime(1748): Caused by: java.lang.RuntimeException: Unable to create service com.help.stressfree.mediabrowser.MusicService: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2697)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.access$1700(ActivityThread.java:159)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1404)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.os.Looper.loop(Looper.java:176)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.main(ActivityThread.java:5419)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at java.lang.reflect.Method.invokeNative(Native Method)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at java.lang.reflect.Method.invoke(Method.java:525)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at dalvik.system.NativeStart.main(Native Method)
06-07 09:59:37.000: E/AndroidRuntime(1748): Caused by: java.lang.IllegalArgumentException: MediaButtonReceiver component may not be null.
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.support.v4.media.session.MediaSessionCompat$MediaSessionImplBase.<init>(MediaSessionCompat.java:1054)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:176)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.support.v4.media.session.MediaSessionCompat.<init>(MediaSessionCompat.java:118)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at com.help.stressfree.mediabrowser.MusicService.onCreate(MusicService.java:169)
06-07 09:59:37.000: E/AndroidRuntime(1748):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2687)
06-07 09:59:37.000: E/AndroidRuntime(1748):     ... 10 more

My overriden Unity activity has this in onCreate

protected void onCreate(Bundle savedInstanceState) {

        // call UnityPlayerActivity.onCreate()
        super.onCreate(savedInstanceState);

        Instance = this;

        LogHelper.d(TAG, "ThriveUnityPlayerActivity.onCreate");

        //Intent intent = new Intent(this, MusicPlayerActivity.class);
        //startActivity(intent);

        Intent intent = new Intent(this, MusicService.class);
        startService(intent);
    }

And my MusicService class has the following onCreate function

@Override
     public void onCreate() {
         super.onCreate();
         LogHelper.d(TAG, "onCreate");

         //mPlayingQueue = new ArrayList<>();
         //mMusicProvider = new MusicProvider();
         //mPackageValidator = new PackageValidator(this);

         mMyServiceHandler = new Handler()
         {
             //here we will receive messages from activity(using sendMessage() from activity)
             public void handleMessage(Message msg)
             {
                 LogHelper.i(TAG,"handleMessage(Message msg)" );
                 switch(msg.what)
                 {
                     case 0:
                         PlayTrackByName((String) msg.obj);

                         break;

                     default:
                         break;
                 }
             }
         };

         // Start a new MediaSession
         mSession = new MediaSessionCompat(this, "MusicService");
         setSessionToken(mSession.getSessionToken());
         mSession.setCallback(new MediaSessionCallback());
         mSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
             MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);

         mPlayback = new Playback(this/*, mMusicProvider*/);
         mPlayback.setState(PlaybackStateCompat.STATE_NONE);
         mPlayback.setCallback(this);
         mPlayback.start();

         Context context = getApplicationContext();
         Intent intent = new Intent(context, MusicPlayerActivity.class);
         PendingIntent pi = PendingIntent.getActivity(context, 99 /*request code*/,
                 intent, PendingIntent.FLAG_UPDATE_CURRENT);
         mSession.setSessionActivity(pi);

         Bundle extras = new Bundle();
         //CarHelper.setSlotReservationFlags(extras, true, true, true);
         mSession.setExtras(extras);

         updatePlaybackState(null);

         mMediaNotificationManager = new MediaNotificationManager(this);
     }

Has anyone any idea what the crash means and how I would go about fixing it?

like image 861
Real World Avatar asked Jun 07 '17 09:06

Real World


3 Answers

Try adding a media button receiver in manifest under the activity that controls the media I experienced this running 4.4, but not on higher versions.

<receiver android:name="android.support.v4.media.session.MediaButtonReceiver" >
<intent-filter>
  <action android:name="android.intent.action.MEDIA_BUTTON" />
</intent-filter>
</receiver>

and read up; Media Button Receiver

like image 56
Thadeus Ajayi Avatar answered Nov 20 '22 05:11

Thadeus Ajayi


I have the same issue because of my mistaken.I defined two MEDIA_BUTTON action in AndroidManifest.xml.

<receiver android:name="android.support.v4.media.session.MediaButtonReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
        </intent-filter>
    </receiver>
    <receiver android:name=".receiver.MediaButtonIntentReceiver">
        <intent-filter>
            <action android:name="android.intent.action.MEDIA_BUTTON" />
            <action android:name="android.media.AUDIO_BECOMING_NOISY" />
        </intent-filter>
    </receiver>

So I delete one of them and error dismissed.

like image 44
turbofan Avatar answered Nov 20 '22 07:11

turbofan


Update AndroidX

<receiver android:name="androidx.media.session.MediaButtonReceiver" >
   <intent-filter>
        <action android:name="android.intent.action.MEDIA_BUTTON" />
   </intent-filter>
</receiver>
like image 1
Zero Avatar answered Nov 20 '22 07:11

Zero