尝试在空对象上调用接口方法“android.media.session.ISessionController android.media.session.ISession.getController(
Posted
技术标签:
【中文标题】尝试在空对象上调用接口方法“android.media.session.ISessionController android.media.session.ISession.getController()”【英文标题】:Attempt to invoke interface method 'android.media.session.ISessionController android.media.session.ISession.getController()' on a null object 【发布时间】:2021-07-06 14:49:23 【问题描述】:以下代码适用于带有使用媒体会话控件的音乐通知播放器。每当我单击通知中的控件时,它都会因上述错误而崩溃。如果我错了,请查看下面的代码并纠正我。在下面的代码中,我使用了媒体会话和广播接收器来构建通知。 “track”是我所有歌曲数据的模型类。
try
if (track.getImage() == null)
track.setImage(BitmapFactory.decodeResource(context.getResources(),
R.mipmap.ic_launcher));
NotificationManagerCompat notificationManagerCompat = NotificationManagerCompat.from(context);
MediaSessionCompat mediaSessionCompat = new MediaSessionCompat(context, "tag");
mediaSessionCompat.setMetadata(
new MediaMetadataCompat.Builder()
.putString(MediaMetadata.METADATA_KEY_TITLE, track.getTitle())
.putString(MediaMetadata.METADATA_KEY_ARTIST, track.getArtist())
.putBitmap(MediaMetadata.METADATA_KEY_ALBUM_ART, track.getImage())
.putString(MediaMetadata.METADATA_KEY_ALBUM, track.getAlbum())
.build()
);
mediaSessionCompat.setFlags(
MediaSessionCompat.FLAG_HANDLES_MEDIA_BUTTONS |
MediaSessionCompat.FLAG_HANDLES_TRANSPORT_CONTROLS);
PlaybackStateCompat.Builder stateBuilder = new PlaybackStateCompat.Builder();
stateBuilder.setActiveQueueItemId(MediaSession.QueueItem.UNKNOWN_ID);
long actions = PlaybackStateCompat.ACTION_PLAY_PAUSE | PlaybackStateCompat.ACTION_STOP | PlaybackStateCompat.ACTION_SKIP_TO_NEXT | PlaybackStateCompat.ACTION_SKIP_TO_PREVIOUS;
stateBuilder.setActions(actions);
if (isPlaying)
stateBuilder.setState(PlaybackStateCompat.STATE_PLAYING, 0, 1.0f);
mediaSessionCompat.setActive(true);
else
stateBuilder.setState(PlaybackStateCompat.STATE_PAUSED, 0, 1.0f);
mediaSessionCompat.setActive(true);
mediaSessionCompat.setPlaybackState(stateBuilder.build());
Bitmap icon = track.getImage();
PendingIntent pendingIntentPrevious;
int drw_previous;
// pendingIntentPrevious = null;
// drw_previous = 0;
Intent intentPrevious = new Intent(context, NotificationActionService.class)
.setAction(ACTION_PREVIUOS);
pendingIntentPrevious = PendingIntent.getBroadcast(context, 0,
intentPrevious, PendingIntent.FLAG_UPDATE_CURRENT);
drw_previous = R.drawable.ic_back;
Intent intentPlay = new Intent(context, NotificationActionService.class)
.setAction(ACTION_PLAY);
PendingIntent pendingIntentPlay = PendingIntent.getBroadcast(context, 0,
intentPlay, PendingIntent.FLAG_UPDATE_CURRENT);
PendingIntent pendingIntentNext;
int drw_next;
// pendingIntentNext = null;
// drw_next = 0;
Intent intentNext = new Intent(context, NotificationActionService.class)
.setAction(ACTION_NEXT);
pendingIntentNext = PendingIntent.getBroadcast(context, 0,
intentNext, PendingIntent.FLAG_UPDATE_CURRENT);
drw_next = R.drawable.ic_next;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.logo)
.setContentTitle(track.getTitle())
.setContentText(track.getArtist())
.setLargeIcon(icon)
.setAutoCancel(isPlaying ? false : true)
.setOngoing(isPlaying ? true : false)
.setWhen(0)
.setNotificationSilent()
.setSound(null)
.addAction(drw_previous, "Previous", pendingIntentPrevious)
.addAction(playbutton, "Play", pendingIntentPlay)
.addAction(drw_next, "Next", pendingIntentNext)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat.getSessionToken()))
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
else
notification = new NotificationCompat.Builder(context, CHANNEL_ID)
.setSmallIcon(R.drawable.logo)
.setContentTitle(track.getTitle())
.setContentText(track.getArtist())
.setLargeIcon(icon)
.setAutoCancel(true)
.setOngoing(false)
.setWhen(0)
.setNotificationSilent()
.setSound(null)
.addAction(drw_previous, "Previous", pendingIntentPrevious)
.addAction(playbutton, "Play", pendingIntentPlay)
.addAction(drw_next, "Next", pendingIntentNext)
.setStyle(new androidx.media.app.NotificationCompat.MediaStyle()
.setShowActionsInCompactView(0, 1, 2)
.setMediaSession(mediaSessionCompat.getSessionToken()))
.setPriority(NotificationCompat.PRIORITY_LOW)
.build();
notificationManagerCompat.notify(1, notification);
catch (Exception e)
Log.e("media sesison", e.getLocalizedMessage());
【问题讨论】:
这能回答你的问题吗? Android WebView push notification? @frankenapps 不,我正在寻找具有播放暂停控件并通过应用通知生成器处理的媒体播放通知。 【参考方案1】:有一个内部 Android MediaSessions 限制 SESSION_CREATION_LIMIT_PER_UID = 100;
您应该释放您不再需要的 MediaSession
实例。
如何重现:
// Just create 100 instances of MediaSession
repeat(200)
val session = MediaSessionCompat(context, "Session") // Will cause a crash after 99 iterations
如何解决:
// You should release MediaSession instances that you don't need anymore.
repeat(200)
val session = MediaSessionCompat(context, "Session")
session.release()
【讨论】:
【参考方案2】:我也遇到了同样的问题,我用下面的方法解决了。
你应该在创建会话时调用 setActive(true) 并在销毁时释放它。
【讨论】:
以上是关于尝试在空对象上调用接口方法“android.media.session.ISessionController android.media.session.ISession.getController(的主要内容,如果未能解决你的问题,请参考以下文章
java.lang.NullPointerException:尝试在空对象引用上调用接口方法“OnColorChangeListener.colorChanged(java.lang.String)”
尝试在空对象上调用接口方法“android.media.session.ISessionController android.media.session.ISession.getController(
错误:java.lang.NullPointerException:尝试在空对象引用上调用接口方法“int java.util.List.size()”
尝试在空对象引用上调用接口方法“java.lang.Object java.util.Map$Entry.getValue()”
NullPointerException:尝试在空对象引用上调用虚拟方法 findViewById(int)'
如何删除错误以在空对象引用上调用接口方法“boolean android.database.Cursor.moveToFirst()”