Android:媒体浏览器客户端上的播放状态返回空指针异常
Posted
技术标签:
【中文标题】Android:媒体浏览器客户端上的播放状态返回空指针异常【英文标题】:Android: Playback State on Media Browser Client returns null pointer exception 【发布时间】:2017-09-24 03:38:02 【问题描述】:我目前正在使用来自 android 支持库的 MediaBrowser compat 使用客户端/服务方法开发一个简单的媒体播放器。
我收到以下异常:
FATAL EXCEPTION: main
Process: valdivia.peter.inclubooks, PID: 32383
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.support.v4.media.session.PlaybackStateCompat.getState()' on a null object reference
at valdivia.peter.inclubooks.activities.MostrarAudiolibroActivity$3.onClick(MostrarAudiolibroActivity.java:118)
at android.view.View.performClick(View.java:5201)
at android.view.View$PerformClick.run(View.java:at android.os.Handler.handleCallback(Handler.java:
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
我的理解是控制器或播放状态都为空,所以显然它不能返回代表播放器状态的 int。
以下是我如何在 MediaBrowser 回调的 onConnected() 方法中的客户端活动上创建控制器:
@Override
public void onConnected()
super.onConnected();
Log.d(tag,"Conecté al servicio");
Log.d(tag,"token:" + mMediaBrowser.getSessionToken().toString());
try
controller = new MediaControllerCompat(getApplicationContext(),mMediaBrowser.getSessionToken());
catch(RemoteException exception)
Log.d(tag,"Remote Exception al crear el Media Controller");
Toast.makeText(getApplicationContext(),"Error al crear los controles de la sesión",Toast.LENGTH_LONG).show();
return;
controller.registerCallback(mControllerCallback);
MediaControllerCompat.setMediaController(MostrarAudiolibroActivity.this,controller);
buildTransportControls();
以及我如何构建传输控件:
private void buildTransportControls()
ImageButton mBotonPausa = (ImageButton) findViewById(R.id.mostrar_audiolibro_playpause);
mBotonPausa.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
MediaControllerCompat mediaControllerCompat = MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this);
int estado = mediaControllerCompat.getPlaybackState().getState();
if(estado == PlaybackStateCompat.STATE_PLAYING)
MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().pause();
else if (estado == PlaybackStateCompat.STATE_PAUSED)
MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().play();
else
Bundle extras = new Bundle();
extras.putString("titulo",MostrarAudiolibroActivity.this.audiolibro.getTitulo());
extras.putString("autor",MostrarAudiolibroActivity.this.audiolibro.getAutor());
MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().playFromUri(Uri.parse(MostrarAudiolibroActivity.this.audiolibro.getLinkAudiolibro()),extras);
Log.d(tag,"Voy a tratar de abrir el audio");
);
ImageButton mBotonReiniciar = (ImageButton) findViewById(R.id.mostrar_audiolibro_reiniciar);
mBotonReiniciar.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().seekTo(0);
//MediaControllerCompat.getMediaController(MostrarAudiolibroActivity.this).getTransportControls().play();
);
我做错了什么。提前致谢。
【问题讨论】:
【参考方案1】:对MediaControllerCompat.getPlaybackState()
的调用可以并且正在返回null。 documentation for that method 表示返回 null 是可能的。您必须更改您的代码以允许这种可能性。
更具体地说,行:
int estado = mediaControllerCompat.getPlaybackState().getState();
必须拆分成多个部分:
PlaybackStateCompat playbackStateCompat = mediaControllerCompat.getPlaybackState();
if (playbackStateCompat == null)
// handle the case that it's null
else
int estado = playbackStateCompat.getState();
【讨论】:
以上是关于Android:媒体浏览器客户端上的播放状态返回空指针异常的主要内容,如果未能解决你的问题,请参考以下文章