TaskStackBuilder getPendingIntent 方法不起作用

TaskStackBuilder getPendingIntent method doesn't work

不确定具体问题是什么,但下面的代码在完成之前停止并且没有进入 Log.d("debug", "debug 2.4.3");

    NotificationCompat.Action actionViewContact, actionCreateActivity;

    Class createContact = ViewContactsActivity.class;
    Class viewContacts  = ViewContactsActivity.class;
    Class createActivity= CreateActivityActivity.class;

    //Activities to launch via notifications
    Intent createContactIntent  = new Intent(context, createContact);
    Intent viewContactsIntent   = new Intent(context, viewContacts);
    Intent createActivityIntent = new Intent(context, createActivity);

    //Put Extras
    createContactIntent.putExtra(ViewContactsActivity.CREATE_CONTACT_KEY, true);
    createContactIntent.putExtra(ViewContactsActivity.ANI_KEY, ani);
    createContactIntent.putExtra(MainActivity.ONSTART_KEY, MainActivity.ONSTART_CREATE_CONTACT);

    viewContactsIntent.putExtra(ViewContactsActivity.CONTACTS_KEY, (ArrayList)contacts);
    viewContactsIntent.putExtra(ViewContactsActivity.ANI_KEY, ani);
    viewContactsIntent.putExtra(MainActivity.ONSTART_KEY, MainActivity.ONSTART_VIEW_CONTACTS);

    createActivityIntent.putExtra(CreateActivityActivity.CONTACT_KEY, contacts.get(0));
    createActivityIntent.putExtra(CreateActivityActivity.ANI_KEY, contacts.get(0));
    createActivityIntent.putExtra(MainActivity.ONSTART_KEY, MainActivity.ONSTART_CREATE_ACTIVITY);

    //Add backstack, so that back button results in returning to MainActivity.class
    TaskStackBuilder sbCreateContact  = TaskStackBuilder.create(context);
    TaskStackBuilder sbViewContacts   = TaskStackBuilder.create(context);
    TaskStackBuilder sbCreateActivity = TaskStackBuilder.create(context);

    sbCreateContact.addParentStack(createContact).addNextIntent(createContactIntent);
    sbViewContacts.addParentStack(viewContacts).addNextIntent(viewContactsIntent);
    sbCreateActivity.addParentStack(createActivity).addNextIntent(createActivityIntent);

    //Create pending intents for notification actions
    int piFlag = PendingIntent.FLAG_UPDATE_CURRENT;
    Log.d("debug", "debug 2.4.1");
    PendingIntent piCreateContact = sbCreateContact.getPendingIntent(0, piFlag);
    Log.d("debug", "debug 2.4.2");
    PendingIntent piViewContacts  = sbViewContacts.getPendingIntent(0, piFlag);
    Log.d("debug", "debug 2.4.3");
    PendingIntent piCreateActivity= sbCreateActivity.getPendingIntent(0, piFlag);
    Log.d("debug", "debug 2.4.4");

我正在尝试更深入地研究它,但这确实是一个奇怪的错误。

我能够解决问题。它位于 viewContactsIntent.putExtra(ViewContactsActivity.CONTACTS_KEY, (ArrayList)contacts); 行。如果您将对象不正确地捆绑到意图中,然后尝试调用 getPendingIntent(),程序将崩溃并且无法显示错误 report/logs。我翻遍了源代码,发现了几个空的 catch 块。

实际解决方案涉及让 Contact class 正确实施 Parcable。