Agent67
Honorary Master
So for my final assignment of the year, we have to make a Bingo App with three screens. I've made good progress so far, but for some reason all of a sudden, the buttons on my second screen aren't working at all. Even tho I don't see anything wrong with their on-click methods at a quick glance. The other screens' buttons work, but it's just this one's buttons that aren't firing. I'm not sure how I can fix this...
The code:
The code:
Java:
// The screen in question
public class DisplayActivity extends AppCompatActivity {
private static final String BINGO_HISTORY_FILE = "bingohistory.txt";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display);
// Get the contents of the array from the MainActivity and display them
TextView txtCurrent = (TextView) findViewById(R.id.txtCurrent);
int[] NumbersCalled = MainActivity.NumbersCalled;
final String outputString = getOutputString(NumbersCalled);
txtCurrent.setText("Numbers called: " + outputString);
// Event handler for the Go Back to Main Menu button
Button returnBtn = (Button) findViewById(R.id.btnReturn);
returnBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MainActivity.NumbersCalled = NumbersCalled;
DisplayActivity.this.finish();
startActivity(new Intent(DisplayActivity.this, MainActivity.class));
}
});
// Event handler for the Save button
Button saveBtn = (Button) findViewById(R.id.btnReturn);
saveBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Save the current bingo numbers after the game is finished
writeToFile(outputString);
}
});
}
private String getOutputString(int[] NumbersCalled) {
String temp = "";
for (int i = 0; i < NumbersCalled.length; i++) {
temp += NumbersCalled[i] + " ";
}
final String outputString = temp;
return outputString;
}
protected void writeToFile(String data) {
// Write the current numbers to the "bingohistory.txt" text file.
// Check for exceptions when attempting to write to the file
FileOutputStream file = null;
try {
file = openFileOutput(BINGO_HISTORY_FILE,
DisplayActivity.this.MODE_PRIVATE);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(DisplayActivity.this,
"Couldn't find file", Toast.LENGTH_LONG).show();
}
OutputStreamWriter osw = new OutputStreamWriter(file);
BufferedWriter bw = new BufferedWriter(osw);
try {
bw.write(data);
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(DisplayActivity.this,
"Couldn't write to file", Toast.LENGTH_LONG).show();
}
try {
bw.newLine();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(DisplayActivity.this,
"Couldn't write to file", Toast.LENGTH_LONG).show();
}
try {
bw.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(DisplayActivity.this,
"Couldn't write to file", Toast.LENGTH_LONG).show();
}
try {
osw.close();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(DisplayActivity.this,
"Couldn't write to file", Toast.LENGTH_LONG).show();
}
}
}
// The Main Menu's screen
public class MainActivity extends AppCompatActivity {
// Declare class-level variables
public static int currentNumber;
public static int[] NumbersCalled = {currentNumber};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the first number to be displayed. Save the number to be displayed
// in the NumbersCalled array and display the number in the textview
TextView txtCurrentNumber = (TextView) findViewById(R.id.txtNumber);
getAndSaveNewNumber();
txtCurrentNumber.setText(String.valueOf(currentNumber));
// Event handler for the Exit button
Button exitAppBtn = (Button) findViewById(R.id.btnExit);
exitAppBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Finish and exit the app
MainActivity.this.finish();
System.exit(0);
}
});
// Event handler for the Next button
Button nextBtn = (Button) findViewById(R.id.btnNext);
nextBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// Get the next number to be displayed. Save the number to be displayed
// in the NumbersCalled array and display the number in the textview
getAndSaveNewNumber();
txtCurrentNumber.setText(String.valueOf(currentNumber));
}
});
// Event handler for the Bingo button
Button bingoBtn = (Button) findViewById(R.id.btnBingo);
bingoBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DisplayActivity.class).putExtra("NumbersCalled", NumbersCalled));
}
});
// Event handler for the Display button
Button displayBtn = (Button) findViewById(R.id.btnDisplay);
displayBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DisplayActivity.class).putExtra("NumbersCalled", NumbersCalled));
}
});
// Event handler for the History button
Button historyBtn = (Button) findViewById(R.id.btnHistory);
historyBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, HistoryActivity.class));
}
});
}
// Generate a new random number and save it to the NumbersCalled array
// Then proceed to sort the array in ascending order
public void getAndSaveNewNumber() {
currentNumber = (int)Math.floor(Math.random() * (99 - 0 + 1) + 0);
//Check if number has been called before, if so, don't add to the array nor display it.
if (checkNumbersCalled(currentNumber)) {
currentNumber = (int)Math.floor(Math.random() * (99 - currentNumber + 1) + 0);
saveNewNumber();
} else {
saveNewNumber();
}
// Sort the array in asc order
Arrays.sort(NumbersCalled);
}
// Go through the NumbersChecked array and check if n is already in the array
public boolean checkNumbersCalled(int n) {
for (int i = 0; i < NumbersCalled.length; i++) {
if (NumbersCalled[i] == currentNumber) {
return true;
}
}
return false;
}
// Add a number to the array
public void saveNewNumber() {
// Add number to the array
// Create a new array with the current number
// then substitute the NumbersCalled array's value with the new array
int n = NumbersCalled.length;
int[] NewArray = new int[n + 1];
for (int i = 0; i < n; i++) {
NewArray[i] = NumbersCalled[i];
}
NewArray[n] = currentNumber;
NumbersCalled = NewArray;
}
}
}