user8284493
user8284493

Reputation:

App crashes after button click

I created an Android app which saves the score of two teams. Both teams has buttons to give them 1, 2 or 3 points. And there is a reset button which resets both teams score to 0. It looks like this.

When I click on Reset (both teams has 0 score) nothing happens, but when I click on a button which should add points to a team, the app crashes.

First of all I defined the scores of the teams (in global scope of course).

int teamAScore = 0;
int teamBScore = 0;

Then I wrote methods for adding points to the score. For example adding 3 points for Team A.

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

The updateA() method refreshes Team A's score.

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

But exactly here the app crashes. It crashes when I try to add points to a team. But when I reset the points (both scores are 0) nothing happens.

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

The problem could be a NullPointerException, I am not sure but I hope you can help me at this. I am still new to Android programming.

Java code:

package com.example.android.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.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 code:

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

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

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

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

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

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

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

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

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

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

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

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

Upvotes: 0

Views: 87

Answers (2)

umair
umair

Reputation: 161

change methods access from private to public

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

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

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

    public void addTeamB3(View view){
        teamBScore += 3;
        updateB(teamBScore);// teamBScore
    }

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

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

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

as others have mentioned change integer value of score to string

Team A

ascore.setText(Integer.toString(score));

Team B

bscore.setText(Integer.toString(score));

and you forgot to call reset method from xml, add following to reset button

 android:onClick="reset"

Upvotes: 1

Patrick Zinner
Patrick Zinner

Reputation: 364

The Problem is, that ascore.setText(score) passes an integer to the setText method. The setText(int) method looks for a resource with the given id. You need to parse your points to a String. Try ascore.setText(Integer.toString(score)) instead

Upvotes: 0

Related Questions