[英]Android Button onClick XML not working
Eclipse doesn't like my addLifeForP1 method that is called by the onClick of the the PlusLifeForP1Button. Eclipse不喜欢PlusLifeForP1Button的onClick调用的addLifeForP1方法。 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!
预期-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>
您缺少onCreate方法末尾的大括号。
Paste you addLifeForP1 outside onCreate function. 将addLifeForP1粘贴到onCreate函数之外。 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);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.