Attempt to invoke interface method #39;android.media.session.ISessionController android.media.session.ISession.getController()#39; on a null object(尝试在空对象上调用接口方法android.media.ession.ISessionController android.media.session.ISession.getController()#39;) -
本文介绍了尝试在空对象上调用接口方法android.media.ession.ISessionController android.media.session.ISession.getController()';的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
下面的代码是用于音乐通知播放器的,带有使用媒体会话的控件。每当我从通知中单击控件时,它就会崩溃,并出现上述错误。请看下面的代码,如果我错了,请纠正我。在下面的代码中,我使用了媒体会话和广播接收器来构建通知。";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 MediaSession限制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()
}
这篇关于尝试在空对象上调用接口方法android.media.ession.ISessionController android.media.session.ISession.getController()';的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:尝试在空对象上调用接口方法android.media.ession.ISessionController android.media.session.ISession.getController()';


猜你喜欢
- 想使用ViewPager,无法识别android.support.*? 2022-01-01
- Android - 我如何找出用户有多少未读电子邮件? 2022-01-01
- Android viewpager检测滑动超出范围 2022-01-01
- 如何检查发送到 Android 应用程序的 Firebase 消息的传递状态? 2022-01-01
- android 4中的android RadioButton问题 2022-01-01
- MalformedJsonException:在第1行第1列路径中使用JsonReader.setLenient(True)接受格式错误的JSON 2022-01-01
- 使用自定义动画时在 iOS9 上忽略 edgesForExtendedLayout 2022-01-01
- 在测试浓缩咖啡时,Android设备不会在屏幕上启动活动 2022-01-01
- Android - 拆分 Drawable 2022-01-01
- 用 Swift 实现 UITextFieldDelegate 2022-01-01