开发者

Android Button onClick XML not working

Eclipse doesn't like my addLifeForP1 method that is called by the onClick of the the PlusLifeForP1Button. It says the following about the method call: - Syntax error on token "(", ; expected - Syntax error on token ")", ; expected - void is an invalid type for the variable Please let me know if you can figure this out!

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);
            lifeTo开发者_如何转开发tal++;
            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>


You're missing the end brace at the end of your onCreate method.


Paste you addLifeForP1 outside onCreate function. This will solve the issue.

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);      
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜