如何在布局中添加@OnClick从buttom

时间:2017-03-22 13:27:11

标签: android android-linearlayout butterknife

我使用ButterKnife来简化生活,我在LinearLayout中有一个按钮,我希望在其上包含@OnClick注释。

#include <stdlib.h>
#include <stdio.h>

struct node {
    int data;
    struct node *next; 
};

void push(struct node **headRef, int data) {
    struct node *newNode = malloc(sizeof(struct node));
    newNode->data = data;
    newNode->next = *headRef;
    *headRef = newNode;
}

void removeDuplicates(struct node **head) {
        struct node *currentNode = *head; //The node we compare other nodes against
    struct node *runningNode = (*head)->next; //The node we are comparing to
    struct node *runningNodePrev = *head; //The node before the node we are comparing to
    struct node *runningNodeNext = (*head)->next->next; // The node after the node we are comparing to
    int count = -1;
    while (currentNode->next != NULL) { //Ensure we are not looking at the last node -- cannot have comparisons past this node
        count++;
        if (count) {
            //'Base Position'
            currentNode = currentNode->next;
            runningNodePrev = currentNode;
            runningNode = currentNode->next;
            runningNodeNext = runningNode->next;
        }
        printf("\nChecking for a match with  %d ... \n", currentNode->data);
        while (runningNode != NULL) { //Compare each node in the list until we reach the end of the list
            printf("Inspecting next adjacent node ... \n");
            if (runningNode->data == currentNode->data) {//If a duplicate is found
                printf("Found match ... ");

                //Ensure link is maintained before removing duplicate node
                if (currentNode == runningNodePrev)
                    currentNode->next = runningNodeNext;
                runningNodePrev->next = runningNodeNext; 

                free(runningNode);//Delete duplicate node
                printf("Deleted duplicate.\n");
            }
            runningNode = runningNodeNext;//Continue searching for duplicates at the first unchecked node
            runningNodeNext = runningNodeNext->next;//Move running node leader up the list.     
        }
    }
}

int main(void) {
    struct node *t1 = NULL;
    struct node *t2 = NULL;
    struct node *t4 = NULL;
    struct node *t5 = NULL;
    push(&t1, 1);
    push(&t1, 1);
    push(&t1, 1);
    push(&t1, 1);
    push(&t2, 6);
    push(&t2, 1);
    push(&t2, 2);
    push(&t2, 3);
    push(&t2, 4);
    push(&t2, 5);
    push(&t2, 6);
    push(&t4, 4);
    push(&t4, 2);
    push(&t4, 3);
    push(&t4, 2);
    push(&t4, 1);
    push(&t5, 0);
    push(&t5, 0);
    push(&t5, 1);
    printf("Testing removeDuplicates()...\n");
    printf("Case 1: Calling removeDuplicates({1,0,0}).\n");
    printf("Expected result: { 1 0 }\n");
    printf("Actual result:   { ");
    removeDuplicates(&t5);
    ptr = t5;
    while (ptr != NULL) { 
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("}\n");
    printf("Case 2: Calling removeDuplicates({1,2,3,2,4}).\n");
    printf("Expected result: { 1 2 3 4 }\n");
    printf("Actual result:   { ");
    removeDuplicates(&t4);
    ptr = t4;
    while (ptr != NULL) {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("}\n");
    printf("Case 3: Calling removeDuplicates({6,5,4,3,2,1,6}).\n");
    printf("Expected result: { 6 5 4 3 2 1 }\n");
    printf("Actual result:   { ");
    removeDuplicates(&t2);
    ptr = t2;
    while (ptr != NULL) {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("}\n");
    printf("Case 4: Calling removeDuplicates({1,1,1,1}).\n");
    printf("Expected result: { 1 }\n");
    printf("Actual result:   { ");
    removeDuplicates(&t1);
    ptr = t1;
    while (ptr != NULL) {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("}\n");
}

使用此布局时,它会抱怨找不到按钮ID。

<LinearLayout
    android:id="@+id/buttons_container"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="30dp"
    android:layout_centerHorizontal="true">
    <Button
        android:id="@+id/cancel_button"
        style="@style/PaymentButtonStyle"
        android:text="calncel@string/cancel_label"
        android:textSize="15sp"/>
  </LinearLayout>

我需要做什么?

2 个答案:

答案 0 :(得分:2)

您的ID为cancel_button,而不是cancel_payment_button

并且不要忘记使用ButterKnife.bind()方法绑定ButterKnife。

答案 1 :(得分:0)

我认为找不到具有正确button的{​​{1}},在您上面列出的布局中,id没有id,只有{{1}另外,检查R.id.cancel_payment_button类的导入是否正确(跟随主包)。