Issue
I'm currently trying to make a simple app where I have a strike, ball, and reset button. When the strike and ball button are pressed, then the counters will add 1. If the strike counter hits 3 then it will pop up a message and reset the counters. The same logic applies to the ball counter. The strike and ball counters work perfectly fine, but the reset does not work. I thought it would be the simple lines of code I have below to just reset both counters to 0.
Here is my main activity:
package com.example.umpirebuddy;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
//import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
static final private String TAG = "Umpire Buddy v1.0";
private int strike_count = 0;
private int ball_count = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting onCreate...");
setContentView(R.layout.activity_main);
View StrikeButton = findViewById(R.id.strike_button);
StrikeButton.setOnClickListener(this);
updateStrikeCount();
View BallButton = findViewById(R.id.ball_button);
BallButton.setOnClickListener(this);
updateBallCount();
View ResetButton = findViewById(R.id.reset_button);
BallButton.setOnClickListener(this);
updateStrikeCount();
updateBallCount();
}
private void updateStrikeCount() {
TextView t = (TextView) findViewById(R.id.strike_count_value);
t.setText(Integer.toString(strike_count));
}
private void updateBallCount() {
TextView t = (TextView) findViewById(R.id.ball_count_value);
t.setText(Integer.toString(ball_count));
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.strike_button:
// Start count over if user tries to increment beyond 2.
if (strike_count == 2) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Strike Out");
builder.setMessage("Batter is out!");
builder.setCancelable(false);
builder.setPositiveButton("Next Batter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
strike_count = 0;
updateStrikeCount();
ball_count = 0;
updateBallCount();
}
});
builder.show();
} else {
strike_count++;
}
break;
case R.id.ball_button:
// Start count over if user tries to increment beyond 3.
if (ball_count == 3) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Walk");
builder.setMessage("Batter walks!");
builder.setCancelable(false);
builder.setPositiveButton("Next Batter", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
strike_count = 0;
updateStrikeCount();
ball_count = 0;
updateBallCount();
}
});
builder.show();
} else {
ball_count++;
}
break;
case R.id.reset_button:
ball_count = 0;
updateBallCount();
strike_count = 0;
updateStrikeCount();
break;
}
updateStrikeCount();
updateBallCount();
}
}
Here's my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="@android:color/black"
tools:context=".MainActivity">
<TextView
android:id="@+id/strike_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/strike_label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="30sp"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="145dp" />
<TextView
android:id="@+id/strike_count_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/strike_label"
android:layout_marginStart="34dp"
android:layout_marginLeft="34dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="@id/strike_label"
android:layout_toRightOf="@id/strike_label"
android:text="@string/strike_count_value"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="30sp"
tools:layout_editor_absoluteX="245dp"
tools:layout_editor_absoluteY="225dp" />
<TextView
android:id="@+id/ball_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/strike_label"
android:layout_alignEnd="@+id/strike_label"
android:layout_alignRight="@id/strike_label"
android:layout_marginTop="8dp"
android:layout_marginEnd="22dp"
android:layout_marginRight="22dp"
android:text="@string/ball_label"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="30sp"
tools:layout_editor_absoluteX="165dp"
tools:layout_editor_absoluteY="225dp" />
<TextView
android:id="@+id/ball_count_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/ball_label"
android:layout_marginStart="35dp"
android:layout_marginLeft="35dp"
android:layout_marginTop="0dp"
android:layout_toEndOf="@id/ball_label"
android:layout_toRightOf="@id/ball_label"
android:text="@string/ball_count_value"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
android:textSize="30sp"
tools:layout_editor_absoluteX="245dp"
tools:layout_editor_absoluteY="145dp" />
<Button
android:id="@+id/strike_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ball_label"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="2dp"
android:layout_marginLeft="2dp"
android:layout_marginTop="205dp"
android:layout_marginEnd="-2dp"
android:layout_marginRight="-2dp"
android:text="@string/strike_button_label"
android:textSize="30sp"
tools:layout_editor_absoluteX="138dp"
tools:layout_editor_absoluteY="314dp" />
<Button
android:id="@+id/ball_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/strike_button"
android:layout_alignStart="@+id/strike_button"
android:layout_alignLeft="@id/strike_button"
android:layout_alignEnd="@+id/strike_button"
android:layout_alignRight="@id/strike_button"
android:text="@string/ball_button_label"
android:textSize="30sp"
tools:layout_editor_absoluteX="157dp"
tools:layout_editor_absoluteY="400dp" />
<Button
android:id="@+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/ball_button"
android:layout_alignStart="@id/ball_button"
android:layout_alignLeft="@id/ball_button"
android:layout_alignEnd="@id/ball_button"
android:layout_alignRight="@id/ball_button"
android:text="@string/reset_button_label"
android:textSize="30sp"
tools:layout_editor_absoluteX="148dp"
tools:layout_editor_absoluteY="500dp" />
</RelativeLayout>
Solution
Turns out it was a minor mistake in my MainActivity.java at
View ResetButton = findViewById(R.id.reset_button);
BallButton.setOnClickListener(this);
BallButton should be ResetButton. Pretty silly mistake from copying and pasting previous lines of code. :)
Answered By - SrirachaPapi
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.