MediaBrowserServiceCompat无法绑定到服务

时间:2016-08-27 08:58:13

标签: android android-studio android-service-binding

我的代码非常简单,我只想使用MediaBrowserServiceCompat绑定到音乐服务,实际上我有一个更大的程序有相同的问题所以我试图做一个较小的没有所有不重要的部分,我得到以下错误:

  

08-27 11:31:30.848 12214-12214 / com.example.android.newcheck E / AndroidRuntime:FATAL EXCEPTION:main                                                                                 处理:com.example.android.newcheck,PID:12214                                                                                 java.lang.RuntimeException:无法使用Intent {act = android.media.browse.MediaBrowserService cmp = com.example.android.newcheck / .MusicStreamService}:java绑定到服务com.example.android.newcheck.MusicStreamService@8c043ff。 lang.NullPointerException:尝试在空对象引用上调用接口方法'android.os.IBinder android.support.v4.media.MediaBrowserServiceCompat $ MediaBrowserServiceImpl.onBind(android.content.Intent)'                                                                                     在android.app.ActivityThread.handleBindService(ActivityThread.java:3847)                                                                                     在android.app.ActivityThread.access $ 2200(ActivityThread.java:221)                                                                                     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1887)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                     在android.os.Looper.loop(Looper.java:158)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:7224)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)                                                                                  引起:java.lang.NullPointerException:尝试在空对象引用上调用接口方法'android.os.IBinder android.support.v4.media.MediaBrowserServiceCompat $ MediaBrowserServiceImpl.onBind(android.content.Intent)'                                                                                     在android.support.v4.media.MediaBrowserServiceCompat.onBind(MediaBrowserServiceCompat.java:613)                                                                                     在android.app.ActivityThread.handleBindService(ActivityThread.java:3834)                                                                                     在android.app.ActivityThread.access $ 2200(ActivityThread.java:221)                                                                                     在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1887)                                                                                     在android.os.Handler.dispatchMessage(Handler.java:102)                                                                                     在android.os.Looper.loop(Looper.java:158)                                                                                     在android.app.ActivityThread.main(ActivityThread.java:7224)                                                                                     at java.lang.reflect.Method.invoke(Native Method)                                                                                     在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:1230)                                                                                     在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

我主要在youtube上使用伊恩湖讲座并做了他说的所有事情,但我不知道为什么我得到这个错误,我试图在网上找到答案,但找不到任何答案。 谢谢您的帮助。

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 个答案:

答案 0 :(得分:8)

我忘了在MusicStreamService类的onCreate方法中编写super.onCreate()。解决了。