应用程序在构建通知时崩溃

时间:2017-07-12 20:59:39

标签: android notifications

我为自己写过一个音乐播放器。像所有优秀的球员一样,我希望它能通过一些控制来获得通知。我不记得我在哪里复制了用于构建此类通知的大部分代码,但它在我的旧手机(Android 5)上运行。然而,在我的新手机(Android 6)上,它在builder.setPublicVersion(notification);行崩溃了。你可以在下面说我已经尝试过替换已弃用的setContent。但结果相同。它甚至不会抛出任何东西,它只会崩溃。(我试过try{...}catch(Throwable e){...})。

我按照这样建立通知:

void buildNotification(boolean pausing) {
    NotificationManager manager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
    Notification.Builder builder = new Notification.Builder(this)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setContentTitle("My Player");

    int[] ids = new int[] {
            R.id.imgbtn_pause,
            R.id.imgbtn_next,
            R.id.imgbtn_delete};


    RemoteViews notificationView = new RemoteViews(getPackageName(),R.layout.notification_layout);
    for(int i = 0; i < 3 ; i++) {
        Intent intent = new Intent(this, MyService.class);
        intent.setAction(String.valueOf(i));
        PendingIntent pi = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        notificationView.setOnClickPendingIntent(ids[i],pi);
    }

    //builder.setContent(notificationView);
    builder.setCustomContentView(notificationView);
    Notification notification = builder.build();
    builder.setPublicVersion(notification);
    if(pausing) notificationView.setImageViewResource(R.id.imgbtn_pause,R.mipmap.start);
    else notificationView.setImageViewResource(R.id.imgbtn_pause,R.mipmap.pause);
    notificationView.setTextViewText(R.id.filename, currentFile);
    manager.notify(5,builder.build());
}

LogCat显示

07-14 22:17:26.967 21992-21992/com.example.ginso.myplayer A/libc: Fatal signal 11 (SIGSEGV), code 2, fault addr 0x7ff606e000 in tid 21992 (e.ginso.myplayer)

                                                             [ 07-14 22:17:26.968   458:  458 W/         ]
                                                             debuggerd: handling request: pid=21992 uid=10233 gid=10233 tid=21992

0 个答案:

没有答案