Android按钮onClick XML不起作用

Android按钮onClick XML不起作用,android,button,onclick,Android,Button,Onclick,Eclipse不喜欢我的addLifeForP1方法,它是通过单击plusLifeForP1按钮调用的。它说明了有关方法调用的以下内容: -标记“(”,)上出现语法错误;应为 -令牌“)”上的语法错误;预期 -void是变量的无效类型 如果你能弄明白,请告诉我 public class ActivityTab1 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCrea

Eclipse不喜欢我的addLifeForP1方法,它是通过单击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>
公共类活动选项卡1扩展活动{
创建时的公共void(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);
公共void addLifeForP1(视图){
CharSequence=lifeViewP1.getText();
String String=sequence.toString();
int-lifeTotal=Integer.parseInt(字符串);
lifeTotal++;
lifeViewP1.setText(lifeTotal);
}
}
}

您在onCreate方法的末尾缺少结束大括号。

您在onCreate方法的末尾缺少结束大括号。

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

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

不幸的是,没有。有三个开始括号和三个结束括号。好的,让我们把它们配对。第一个右大括号关闭
addLifeForP1
方法。第二个方法关闭
onCreate
方法。第三个关闭
活动选项卡1
类。唯一的问题是
addLifeForP1
方法在
onCreate
方法之后启动,这意味着它是一个嵌套函数。唯一的问题是什么?Java不允许方法相互嵌套。在
TextView lifeViewP1=(TextView)findViewById(R.id.LifeForP1)之后移动一个右大括号行。不幸的是,没有。有三个开始括号和三个结束括号。好的,让我们把它们匹配起来。第一个右大括号关闭
addLifeForP1
方法。第二个方法关闭
onCreate
方法。第三个关闭
活动选项卡1
类。唯一的问题是
addLifeForP1
方法在
onCreate
方法之后启动,这意味着它是一个嵌套函数。唯一的问题是什么?Java不允许方法相互嵌套。在
TextView lifeViewP1=(TextView)findViewById(R.id.LifeForP1)之后移动一个右大括号行。