Кнопка Android на клике XML не работает - PullRequest
0 голосов
/ 13 апреля 2011

Eclipse не нравится мой метод addLifeForP1, который вызывается onClick кнопки PlusLifeForP1. О вызове метода говорится следующее: - Синтаксическая ошибка на токене "(",; ожидается - синтаксическая ошибка на токене ")",; ожидаемый - void - недопустимый тип для переменной Пожалуйста, дайте мне знать, если вы можете понять это!

public class ActivityTab1 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content1layout);

        Resources res = getResources();
        ImageButton plusLifeButtonP1 = (ImageButton) findViewById(R.id.PlusLifeForP1);
        TextView lifeViewP1 = (TextView) findViewById(R.id.LifeForP1);

        public void addLifeForP1(View view) {
            CharSequence sequence = lifeViewP1.getText();
            String string = sequence.toString();
            int lifeTotal = Integer.parseInt(string);
            lifeTotal++;
            lifeViewP1.setText(lifeTotal);      
        }
    }
    }

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <TextView
        android:label="@+id/LifeForP1"
        android:text="20"
        android:color="#990000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="90px"
        android:layout_y="0px"
        android:textSize="250px"
    />

    <ImageButton
        android:label="@+id/PlusLifeForP1"
        android:background="#ff000000"
        android:src="@drawable/plus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="140px"
        android:layout_y="280px"
        android:onClick="addLifeForP1"
    />

    <ImageButton
        android:label="@+id/MinusLifeForP1"
        android:background="#ff000000"
        android:src="@drawable/minus"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_x="245px"
        android:layout_y="280px"
    />

</AbsoluteLayout>

Ответы [ 2 ]

3 голосов
/ 13 апреля 2011

Вам не хватает конечной скобки в конце метода onCreate.

2 голосов
/ 10 января 2012

Вставьте свой addLifeForP1 вне функции onCreate. Это решит проблему.

public class ActivityTab1 extends Activity {
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedIn`enter code here`stanceState);
        setContentView(R.layout.content1layout);

        Resources res = getResources();
        ImageButton plusLifeButtonP1 = (ImageButton) findViewById(R.id.PlusLifeForP1);
        TextView lifeViewP1 = (TextView) findViewById(R.id.LifeForP1);
    }

    public void addLifeForP1(View view) {
        CharSequence sequence = lifeViewP1.getText();
        String string = sequence.toString();
        int lifeTotal = Integer.parseInt(string);
        lifeTotal++;
        lifeViewP1.setText(lifeTotal);      
    }
}
...