当我创建通知时,会引发以下错误:
02-17 19:15:40.459 8869-8869 E / AndroidRuntime:致命异常:主要 过程:PID:8869 主题:主题:{default = overlay:com.baranovgroup.nstyle, iconPack:com.baranovgroup.nstyle,fontPkg:com.baranovgroup.nstyle, com.android.systemui =叠加:com.baranovgroup.nstyle, com.android.systemui.navbar =叠加:com.baranovgroup.nstyle} android.app.RemoteServiceException:从中发布错误通知 包无法扩展RemoteViews: StatusBarNotification(PKG = com.xxx.www.musicplayer user = UserHandle {0} id = 21 tag = null score = 0 key = 0 | com.xxx.www.musicplayer | 21 | null | 10133:通知(pri = 0 contentView = com.xxx.www.musicplayer / 0x7f04004e vibrate = null sound = null defaults = 0x0 flags = 0x62 color = 0x00000000 vis = PRIVATE)) 在 android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1526) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:148) 在android.app.ActivityThread.main(ActivityThread.java:5461) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:726) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
这是我的代码:
private void createNotification(SongDetail detail){
try {
String songName = detail.getTitle();
String authorName = detail.getArtist();
String albumName = detail.getDisplay_name();
SongDetail audioInfo = MediaController.getInstance().getPlayingSongDetail();
RemoteViews simpleContentView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_small_notification);
RemoteViews expandedView = null;
if (supportBigNotifications){
expandedView = new RemoteViews(getApplicationContext().getPackageName(), R.layout.player_big_notification);
}
Intent intent = new Intent(HomeActivity.APPLICATION_CONTEXT, HomeActivity.class);
intent.setAction(Constants.OPEN_PLAYER);
intent.setFlags(32768);
PendingIntent contentIntent = PendingIntent.getActivity(HomeActivity.APPLICATION_CONTEXT, 0, intent, 0);
Notification notification = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(R.drawable.notification_icon)
.setContentIntent(contentIntent)
.setContentTitle(songName).build();
notification.contentView = simpleContentView;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView = expandedView;
}
setListeners(simpleContentView);
if (supportBigNotifications){
setListeners(expandedView);
}
Bitmap albumArt = audioInfo != null ? audioInfo.getSmallCover(HomeActivity.APPLICATION_CONTEXT) : null;
if (albumArt != null){
notification.contentView.setImageViewBitmap(R.id.player_album_art, albumArt);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView.setImageViewBitmap(R.id.player_album_art, albumArt);
}
}else {
notification.contentView.setImageViewResource(R.id.player_album_art, R.drawable.music);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView.setImageViewResource(R.id.player_album_art, R.drawable.music);
}
}
notification.contentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
notification.contentView.setViewVisibility(R.id.player_next, View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView.setViewVisibility(R.id.player_previous, View.VISIBLE);
notification.bigContentView.setViewVisibility(R.id.player_next, View.VISIBLE);
}
if (MediaController.getInstance().isAudioPaused()){
notification.contentView.setViewVisibility(R.id.player_pause, View.GONE);
notification.contentView.setViewVisibility(R.id.player_play, View.VISIBLE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView.setViewVisibility(R.id.player_pause, View.GONE);
notification.bigContentView.setViewVisibility(R.id.player_play, View.VISIBLE);
}
}else {
notification.contentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
notification.contentView.setViewVisibility(R.id.player_play, View.GONE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView.setViewVisibility(R.id.player_pause, View.VISIBLE);
notification.bigContentView.setViewVisibility(R.id.player_play, View.GONE);
}
}
notification.contentView.setTextViewText(R.id.player_song_name, songName);
notification.contentView.setTextViewText(R.id.player_author_name, authorName);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
notification.bigContentView.setTextViewText(R.id.player_song_name, songName);
notification.bigContentView.setTextViewText(R.id.player_author_name, authorName);
}
notification.flags |= Notification.FLAG_ONGOING_EVENT;
startForeground(21, notification);
if (remoteControlClient != null){
RemoteControlClient.MetadataEditor metadataEditor = remoteControlClient.editMetadata(true);
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_ARTIST, authorName);
metadataEditor.putString(MediaMetadataRetriever.METADATA_KEY_TITLE, songName);
if (audioInfo != null && audioInfo.getCover(HomeActivity.APPLICATION_CONTEXT) != null){
metadataEditor.putBitmap(RemoteControlClient.MetadataEditor.BITMAP_KEY_ARTWORK,
audioInfo.getCover(HomeActivity.APPLICATION_CONTEXT));
}
metadataEditor.apply();
audioManager.requestAudioFocus(this, AudioManager.STREAM_MUSIC, AudioManager.AUDIOFOCUS_GAIN);
}
}catch (Exception e){
Log.e("create Notification",e.toString());
}
}