2

我正在尝试将我的活动连接到MediaBrowserServiceCompat使用MediaBrowserCompat. 这是我的活动:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.bush_did_nine_eleven);
    Log.d("activity", " onCreate");
    mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MusicServiceMediaBrowser.class), new MediaBrowserCompat.ConnectionCallback() {
                @Override
                public void onConnected() {
                    Log.d("activity", "onconnected");
                    super.onConnected();
                }

                @Override
                public void onConnectionFailed() {
                    Log.d("activity", "onConnectionFailed");
                    super.onConnectionFailed();
                }

                @Override
                public void onConnectionSuspended() {
                    Log.d("activity", "onConnectionSuspended");
                    super.onConnectionSuspended();
                }
            }, null);

    mediaBrowser.connect();
    Log.d("activity", "mediaBrowser.connect() called");

}

@Override
protected void onStart() {
    super.onStart();
    Log.d("activity", "onStart");
}

@Override
protected void onStop() {
    super.onStop();
    Log.d("activity", "onStop");
    mediaBrowser.disconnect();
}

您可能希望ConnectionCallback调用其中一种方法,但这是 logcat 打印的内容(包括从服务调用的日志):

09-08 19:28:25.126 13469-13469/le1.mytube D/activity:  onCreate
09-08 19:28:25.135 13469-13469/le1.mytube D/activity: mediaBrowser.connect() called
09-08 19:28:25.137 13469-13469/le1.mytube D/activity: onStart
09-08 19:28:25.154 13469-13469/le1.mytube D/service: onCreate

当我在我的服务中调用时它也会setSessionToken(mediaSession.getSessionToken()) 崩溃NullPointerException

09-08 19:34:04.817 19588-19588/le1.mytube E/AndroidRuntime: FATAL EXCEPTION: main
                                                            Process: le1.mytube, PID: 19588
                                                            Theme: themes:{}
                                                            java.lang.RuntimeException: Unable to create service le1.mytube.services.MusicServiceMediaBrowser: java.lang.NullPointerException: Attempt to invoke interface method 'void android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.setSessionToken(android.support.v4.media.session.MediaSessionCompat$Token)' on a null object reference
                                                                at android.app.ActivityThread.handleCreateService(ActivityThread.java:2921)
                                                                at android.app.ActivityThread.-wrap4(ActivityThread.java)
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1446)
                                                                at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                at android.os.Looper.loop(Looper.java:148)
                                                                at android.app.ActivityThread.main(ActivityThread.java:5461)
                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                             Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'void android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.setSessionToken(android.support.v4.media.session.MediaSessionCompat$Token)' on a null object reference
                                                                at android.support.v4.media.MediaBrowserServiceCompat.setSessionToken(MediaBrowserServiceCompat.java:999)
                                                                at le1.mytube.services.MusicServiceMediaBrowser.onCreate(MusicServiceMediaBrowser.java:46)
                                                                at android.app.ActivityThread.handleCreateService(ActivityThread.java:2911)
                                                                at android.app.ActivityThread.-wrap4(ActivityThread.java) 
                                                                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1446) 
                                                                at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                at android.os.Looper.loop(Looper.java:148) 
                                                                at android.app.ActivityThread.main(ActivityThread.java:5461) 
                                                                at java.lang.reflect.Method.invoke(Native Method) 
                                                                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

<action android:name="android.media.browse.MediaBrowserService"/>如果您想知道的话,我会在我的清单中添加服务所需的内容。

我做错了什么还是只是appcompat(v7:25.3.1)中的一个错误?

4

3 回答 3

2

public IBinder onBind(Intent intent)就我而言,我在服务类中遇到了功能问题。它不应该返回 null。我刚刚删除了override该功能,它解决了这个问题。

于 2018-03-11T03:06:30.280 回答
0

在服务 onCreate 中设置 sessionToken 对我有用。

于 2020-05-14T18:57:35.410 回答
-1

这对我有用:

setSessionToken(mediaSessionCompat.getSessionToken());

@Nullable
@Override
public IBinder onBind(Intent intent) {
    if (SERVICE_INTERFACE.equals(intent.getAction())) {
        return super.onBind(intent);
    }
    return binder;
}
于 2017-11-23T16:27:49.717 回答