问题
My code is very simple, i just want to bind to the music service using MediaBrowserServiceCompat, actually i have a bigger program with the same problem so i tried to make a smaller one without all the unimportant parts and i get the following error:
08-27 11:31:30.848 12214-12214/com.example.android.newcheck E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.android.newcheck, PID: 12214 java.lang.RuntimeException: Unable to bind to service com.example.android.newcheck.MusicStreamService@8c043ff with Intent { act=android.media.browse.MediaBrowserService cmp=com.example.android.newcheck/.MusicStreamService }: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.onBind(android.content.Intent)' on a null object reference at android.app.ActivityThread.handleBindService(ActivityThread.java:3847) at android.app.ActivityThread.access$2200(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.os.IBinder android.support.v4.media.MediaBrowserServiceCompat$MediaBrowserServiceImpl.onBind(android.content.Intent)' on a null object reference at android.support.v4.media.MediaBrowserServiceCompat.onBind(MediaBrowserServiceCompat.java:613) at android.app.ActivityThread.handleBindService(ActivityThread.java:3834) at android.app.ActivityThread.access$2200(ActivityThread.java:221) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1887) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
i mainly used Ian Lake lectures on youtube and did everything he said but i don't know why i get this error, i try to find answers online but couldn't find any answer. thanks for the help.
AndroidManifest:
<service
android:name=".MusicStreamService"
android:exported="true">
<intent-filter>
<action android:name="android.media.browse.MediaBrowserService" />
</intent-filter>
</service>
MainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MediaBrowserCompat mediaBrowser = new MediaBrowserCompat(MainActivity.this, new ComponentName(MainActivity.this, MusicStreamService.class),
new MediaBrowserCompat.ConnectionCallback() {
@Override
public void onConnected() {
Log.v("MainActivity","connected");
}
@Override
public void onConnectionSuspended() {
super.onConnectionSuspended();
}
@Override
public void onConnectionFailed() {
super.onConnectionFailed();
}
}, null);
mediaBrowser.connect();
Button click = (Button) findViewById(R.id.button);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startService(new Intent(MainActivity.this, MusicStreamService.class));
}
});
}
MusicStreamService:
public class MusicStreamService extends MediaBrowserServiceCompat {
@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
Log.v("***********", "getroot");
return new BrowserRoot(getString(R.string.app_name), // Name visible in Android Auto
null); // Bundle of optional extras
}
@Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
Log.v("*********", "one step before connect2");
result.sendResult(null);
}
@Override
public void onCreate() {
MediaSessionCompat mediaSession = new MediaSessionCompat(this, MusicStreamService.class.getSimpleName());
setSessionToken(mediaSession.getSessionToken());
mediaSession.setFlags(MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
mediaSession.setActive(true);
}
回答1:
I forgot to write super.onCreate() in the onCreate method in MusicStreamService class. solved.
来源:https://stackoverflow.com/questions/39179317/mediabrowserservicecompat-unable-to-bind-to-service