应用程序经常在许多手机上崩溃

时间:2018-01-26 05:55:41

标签: java android

  

致命异常:java.lang.IndexOutOfBoundsException:索引:0,大小:   0   my.package.name.NotificationReader.NotificationWearReader(NotificationReader.java:219)    在my.package.namee.NotificationReader.onNotificationPosted(NotificationReader.java:120)

@line 219

if ( !actions.get(i).getRemoteInputs()[0].getLabel()
        .equals(wearableExtender.getActions().get(0).getRemoteInputs()[0].getLabel())) {

代码

public void NotificationWearReader(Notification notification) {

    int flag = 0;

    notificationWear = new NotificationWear();

    NotificationCompat.WearableExtender wearableExtender = new NotificationCompat.WearableExtender(notification);

    if (actions.size() == 0) {
        actions.addAll(wearableExtender.getActions());
    } else
        for (int i = 0; i < actions.size(); i++) {

            if (actions.get(i).getRemoteInputs()[0] == null) {
                flag = 0;
                break;
            }

            if (!actions.get(i).getRemoteInputs()[0].getLabel().equals(wearableExtender.getActions().get(0).getRemoteInputs()[0].getLabel())) {
                flag = 1;
            } else {
                flag = 0;
                break;
            }
        }

    if (flag == 1) {
        actions.addAll(wearableExtender.getActions());
    }

    Log.d("Lines", "actions: " + actions.size());

    for (NotificationCompat.Action act : actions) {
        if (act != null && act.getRemoteInputs() != null) {
            notificationWear.remoteInputs.addAll(Arrays.asList(act.getRemoteInputs()));
            notificationWear.pendingIntent.add(act.actionIntent);

            Log.d(TAG, " Label " + act.getRemoteInputs()[0].getLabel());
            Log.d(TAG, "Bundle " + act.getRemoteInputs()[0].getResultKey());
        }
    }


    notificationWear.bundle = notification.extras;
}

2 个答案:

答案 0 :(得分:0)

试试这个答案

if (actions.size() > 0) {
    for (int i = 0; i < actions.size(); i++) {

        if (actions.get(i).getRemoteInputs()[0] == null) {
            flag = 0;
            break;
        }

        if (!actions.get(i).getRemoteInputs()[0].getLabel().equals(wearableExtender.getActions().get(0).getRemoteInputs()[0].getLabel())) {
            flag = 1;
        } else {
            flag = 0;
            break;
        }
    }
} else{
    actions.addAll(wearableExtender.getActions());
}

答案 1 :(得分:0)

您错过了相应数组和列表的大小和长度检查。这就是为什么你得到一个java.lang.IndexOutOfBoundsException。 如果代码中有条件,请添加更多内容。 改变

if (!actions.get(i).getRemoteInputs()[0].getLabel().equals(wearableExtender.getActions().get(0).getRemoteInputs()[0].getLabel())) {
            flag = 1;
        } else {
            flag = 0;
            break;
        }

if(!actions.get(i).getRemoteInputs().length > 0 && 
         wearableExtender.getActions().size > 0 && 
         wearableExtender.getActions().get(0).getRemoteInputs().length > 0 
         && !actions.get(i).getRemoteInputs()[0].getLabel().equals(
         wearableExtender.getActions().get(0)
         .getRemoteInputs()[0].getLabel()) {
            flag = 1;
        } else {
            flag = 0;
            break;
        }