1

我刚开始使用 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");
    }
}
}

我的服务.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 {
}
}
4

0 回答 0