启动时Android崩溃 - MediaButtonReceiver可能不为null

时间:2017-06-07 09:23:49

标签: java android unity3d

我的Unity应用中出现问题,Android版本在运行Android 4.3的设备上崩溃 该应用程序加载了我为在应用程序暂停时在后台播放音乐而编写的本机插件。它使用Android MediaBrowser API,因此我使用" Compat"版本继续支持以前版本的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

我的重写Unity活动在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);
    }

我的MusicService类具有以下onCreate函数

@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);
     }

有谁知道崩溃意味着什么以及我将如何修复它?

3 个答案:

答案 0 :(得分:12)

尝试在清单中的清单下添加媒体按钮接收器,该活动控制我在运行此4.4版本时遇到的媒体,但不是在更高版本上。

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

并阅读; Media Button Receiver

答案 1 :(得分:1)

由于我的错误,我遇到了同样的问题。我在AndroidManifest.xml中定义了两个MEDIA_BUTTON操作。

<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>

所以我删除其中一个并且错误被驳回。

答案 2 :(得分:1)

更新AndroidX

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