无法执行android的方法:onClick



嗨,我是android开发的新手,点击按钮时无法执行方法。我像教程中那样重新键入了代码,但最终出现了很多错误。检查下面的代码,

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Basketball Score Game"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textSize="25sp"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="10dp"
    android:paddingLeft="70dp"
    android:id="@+id/header"
    />

<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Team A"
        android:layout_marginLeft="50dp"
        android:layout_marginTop="80dp"
        android:textSize="30sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="50sp"
        android:id="@+id/score_a"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="10dp"
        android:onClick="display_score_a"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff6000"
        android:text="Outside The Ring"
        android:onClick="three_pts_a"
        android:padding="10dp"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="40dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:background="#ff6000"
        android:layout_height="wrap_content"
        android:text="Inside the Ring"
        android:layout_marginLeft="40dp"
        android:padding="10dp"
        android:layout_marginTop="10dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:background="#ff6000"
        android:text="Free Throw"
        android:layout_height="wrap_content"
        android:layout_marginLeft="40dp"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        />
</LinearLayout>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Team B"
        android:layout_marginLeft="250dp"
        android:layout_marginTop="80dp"
        android:textSize="30sp"
        />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="50sp"
        android:layout_marginLeft="290dp"
        android:layout_marginTop="10dp"
        />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#ff6000"
        android:text="Outside The Ring"
        android:layout_marginLeft="230dp"
        android:padding="10dp"
        android:layout_marginTop="40dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:background="#ff6000"
        android:text="Inside the Ring"
        android:layout_height="wrap_content"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="10dp"
        android:padding="10dp"
        />
    <Button
        android:layout_width="wrap_content"
        android:background="#ff6000"
        android:layout_height="wrap_content"
        android:layout_marginLeft="230dp"
        android:layout_marginTop="10dp"
        android:text="Free Throw"
        android:padding="10dp"
        />

</LinearLayout>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Reset"
    android:background="#ff6000"
    android:layout_gravity="center_horizontal"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

****Mainactivity.java***

package android.mytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
                int total_pts1;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}
public void three_pts_a(View view)
{
    total_pts1 = total_pts1 + 3;
    display_score_a(total_pts1);
}
private void display_score_a(int number) {
    TextView num = (TextView) findViewById(
            R.id.score_a);
    num.setText(number);
}
}

这是因为整数变量。您必须将其解析为字符串或将该行更改为num.setText(String.valueOf(number));

最新更新