API26上的MediaBrowserServiceCompat

时间:2017-07-11 10:01:53

标签: java android mediabrowserservicecompat

我刚开始使用MediaBrowserServiceCompat为背景音乐播放器开发Android应用程序,并在Android模拟器API 26上获得此异常(不会在API 24上崩溃)。我错过了什么或者我遇到过API 26的错误吗? 谢谢

FATAL EXCEPTION: main
Process: com.aryono01.testapp2, PID: 14907
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object java.lang.ref.WeakReference.get()' on a null object reference
at android.support.v4.media.session.MediaSessionCompat$Callback$StubApi21.onCommand(MediaSessionCompat.java:955)
at android.support.v4.media.session.MediaSessionCompatApi21$CallbackProxy.onCommand(MediaSessionCompatApi21.java:164)
at android.media.session.MediaSession$CallbackMessageHandler.handleMessage(MediaSession.java:1329)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6540)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

MainActivity.java

public class MainActivity extends AppCompatActivity {

private static final String TAG = "Main";
private MediaBrowserCompat mediaBrowser;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MediaBrowserConnectionCallback connectionCallback = new MediaBrowserConnectionCallback();
    mediaBrowser = new MediaBrowserCompat(this, new ComponentName(this, MyService.class), connectionCallback, null);
    mediaBrowser.connect();
}

@Override
protected void onStop() {
    super.onStop();
    mediaBrowser.disconnect();
}

private class MediaBrowserConnectionCallback extends MediaBrowserCompat.ConnectionCallback {
    @Override
    public void onConnected() {
        super.onConnected();
        Log.d(TAG, "onConnected");
        try {
            MediaSessionCompat.Token token = mediaBrowser.getSessionToken();
            MediaControllerCompat mediaController = new MediaControllerCompat(MainActivity.this, token);
            MediaControllerCompat.setMediaController(MainActivity.this, mediaController);
        } catch (RemoteException e) {
            e.printStackTrace();
        }

    }

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

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

MyService.java

public class MyService extends MediaBrowserServiceCompat {
private MediaSessionCompat mediaSession;

@Override
public void onCreate() {
    super.onCreate();

    mediaSession = new MediaSessionCompat(this, "tag");
    setSessionToken(mediaSession.getSessionToken());
    mediaSession.setCallback(new MySessionCallback());
}

@Nullable
@Override
public BrowserRoot onGetRoot(@NonNull String clientPackageName, int clientUid, @Nullable Bundle rootHints) {
    return new BrowserRoot(clientPackageName, null);
}

@Override
public void onLoadChildren(@NonNull String parentId, @NonNull Result<List<MediaBrowserCompat.MediaItem>> result) {
    result.sendResult(null);
}

private class MySessionCallback extends MediaSessionCompat.Callback {
}
}

0 个答案:

没有答案