有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

点击按钮后java应用程序崩溃

我创建了一个Android应用程序,可以保存两队的分数。两队都有按钮给他们1分、2分或3分。还有一个重置按钮,可以将两队的得分重置为0。看起来像这样

当我点击重置(两个团队的分数都为0)时,什么都没有发生,但当我点击一个应该给团队加分的按钮时,应用程序崩溃了

首先,我定义了团队的分数(当然是在全球范围内)

int teamAScore = 0;
int teamBScore = 0;

然后我写了给分数加分数的方法。例如,为A队增加3分

private void addTeamA3(View view){
    teamAScore += 3;
    updateA(teamAScore);
}

updateA()方法刷新A队的得分

private void updateA(int score){
    TextView ascore = (TextView) findViewById(R.id.textView3);
    ascore.setText(score);
}

但就在这里,应用程序崩溃了。当我试图给球队加分时,它崩溃了。但当我重置分数(两个分数都是0)时,什么都没有发生

private void reset(View view){
    teamAScore = 0;
    teamBScore = 0;
    updateA(teamAScore);
    updateB(teamBScore);
}

问题可能是NullPointerException,我不确定,但我希望您能在这方面帮助我。我对Android编程还是新手

Java代码:

package com.example.安卓.justjava;
import 安卓.os.Bundle;
import 安卓.support.v7.app.AppCompatActivity;
import 安卓.view.View;
import 安卓.widget.TextView;

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

int teamAScore = 0;
int teamBScore = 0;

private void addTeamA3(View view){
    teamAScore += 3;
    updateA(teamAScore);
}

private void addTeamA2(View view){
    teamAScore += 2;
    updateA(teamAScore);
}

private void addTeamAFreeThrow(View view){
    teamAScore++;
    updateA(teamAScore);
}

private void addTeamB3(View view){
    teamBScore += 3;
    updateB(teamAScore);
}

private void addTeamB2(View view){
    teamBScore += 2;
    updateB(teamBScore);
}

private void addTeamBFreeThrow(View view){
    teamBScore++;
    updateB(teamBScore);
}

private void reset(View view){
    teamAScore = 0;
    teamBScore = 0;
    updateA(teamAScore);
    updateB(teamBScore);
}


private void updateA(int score){
    TextView ascore = (TextView) findViewById(R.id.textView3);
    ascore.setText(score);
}

private void updateB(int score){
    TextView bscore = (TextView) findViewById(R.id.textView4);
    bscore.setText(score);
}
}

XML代码:

<?xml version="1.0" encoding="utf-8"?>
<安卓.support.constraint.ConstraintLayout 
xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto"
xmlns:tools="http://schemas.安卓.com/tools"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent"
tools:context="com.example.安卓.justjava.MainActivity">

<TextView
    安卓:id="@+id/textView"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginLeft="59dp"
    安卓:layout_marginTop="16dp"
    安卓:text="Team A"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<TextView
    安卓:id="@+id/textView2"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:layout_marginTop="16dp"
    安卓:text="Team B"
    app:layout_constraintTop_toTopOf="parent"
    安卓:layout_marginRight="59dp"
    app:layout_constraintRight_toRightOf="parent" />

<TextView
    安卓:id="@+id/textView3"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="0"
    安卓:textSize="60sp"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    安卓:layout_marginLeft="65dp"
    app:layout_constraintLeft_toLeftOf="parent" />

<TextView
    安卓:id="@+id/textView4"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="0"
    安卓:textSize="60sp"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/textView2"
    安卓:layout_marginRight="65dp"
    app:layout_constraintRight_toRightOf="parent" />

<Button
    安卓:id="@+id/button4"
    安卓:layout_width="120dp"
    安卓:layout_height="wrap_content"
    安卓:text="+3 POINTS"
    安卓:layout_marginLeft="28dp"
    app:layout_constraintLeft_toLeftOf="parent"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/textView3"
    安卓:onClick="addTeamA3"/>

<Button
    安卓:id="@+id/button5"
    安卓:layout_width="120dp"
    安卓:layout_height="wrap_content"
    安卓:text="+2 POINTS"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/button4"
    安卓:layout_marginLeft="28dp"
    app:layout_constraintLeft_toLeftOf="parent"
    安卓:onClick="addTeamA2"/>

<Button
    安卓:id="@+id/button6"
    安卓:layout_width="120dp"
    安卓:layout_height="wrap_content"
    安卓:text="Free throw"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/button5"
    安卓:layout_marginLeft="28dp"
    app:layout_constraintLeft_toLeftOf="parent"
    安卓:onClick="addTeamAFreeThrow"/>

<Button
    安卓:id="@+id/button7"
    安卓:layout_width="120dp"
    安卓:layout_height="wrap_content"
    安卓:text="+3 POINTS"
    安卓:layout_marginRight="28dp"
    app:layout_constraintRight_toRightOf="parent"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/textView4"
    安卓:onClick="addTeamB3"/>

<Button
    安卓:id="@+id/button10"
    安卓:layout_width="120dp"
    安卓:layout_height="wrap_content"
    安卓:text="+2 POINTS"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/button7"
    安卓:layout_marginRight="28dp"
    app:layout_constraintRight_toRightOf="parent"
    安卓:onClick="addTeamB2"/>

<Button
    安卓:id="@+id/button11"
    安卓:layout_width="120dp"
    安卓:layout_height="wrap_content"
    安卓:text="Free throw"
    安卓:layout_marginRight="28dp"
    app:layout_constraintRight_toRightOf="parent"
    安卓:layout_marginTop="16dp"
    app:layout_constraintTop_toBottomOf="@+id/button10"
    安卓:onClick="addTeamBFreeThrow"/>

<Button
    安卓:id="@+id/button12"
    安卓:layout_width="wrap_content"
    安卓:layout_height="wrap_content"
    安卓:text="Reset"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    安卓:layout_marginBottom="57dp" />


共 (1) 个答案

  1. # 1 楼答案

    问题是,ascore.setText(score)将一个整数传递给setText方法。setText(int)方法查找具有给定id的资源。您需要将点解析为字符串。试试ascore.setText(Integer.toString(score))