我正在尝试使用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)?
答案 0 :(得分:1)
就我而言,我在服务类中遇到了 public IBinder onBind(Intent intent)函数的问题。它不应该返回null。我刚刚删除了该函数的覆盖,它解决了这个问题。
答案 1 :(得分:0)
在服务onCreate中设置sessionToken对我有用。
答案 2 :(得分:-1)
对我而言:
setSessionToken(mediaSessionCompat.getSessionToken());
和
@Nullable
@Override
public IBinder onBind(Intent intent) {
if (SERVICE_INTERFACE.equals(intent.getAction())) {
return super.onBind(intent);
}
return binder;
}