I am working on a program in java (android) I would like it to start and continue asking questions, upon button click, until the user is prompted for input, once input is received the program should resume asking the questions. The program should pause when the button is clicked again.
I am new to OOP and self taught. I thought that a thread was going to be the most practical way to solve the issue. I cannot get the program to both loop and allow user input. When trying to use HandlerThread I loose the ability to input data in the EditText. Could someone help with getting this loop to run on start click and loop after input?
I have a functional program that works when the button is cycled:
Main layout begins with "START" button, on click the start button turns to "PAUSE" and a seperate repeat button is made visible (and functional).
The values are generated, the question is asked, and the user is prompted by popping up the soft keyboard and setting the cursor in the EditText field.
Once an answer is received and the "enter/done" keyboard button is clicked, the answer will be evaluated against the saved values. But I cannot get the program to loop, or if I get it to loop it skips input completely and continues to as questions with no time for input.
Please direct me on code cleanliness if needed, I want to learn what I am doing incorrectly.
MAIN.java
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
// Create appropriate objects for buttons, edit text, and text to speech
TextToSpeech tts;
EditText txt;
Button sbtn, rbtn;
// Array and int to store numbers
int[] num;
int added = 0;
// Boolean to check if questions is running
public boolean isRunning;
// Variables for random number range. TODO(Put into switch statement and list to select 1, 10, or 100s)
static int maxNum = 100;
static int minNum = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initializing buttons and others
txt = findViewById(R.id.ans);
sbtn = findViewById(R.id.strButton);
rbtn = findViewById(R.id.rptButton);
rbtn.setVisibility(View.INVISIBLE);
// Initialize text to speech engine
tts = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
tts.setLanguage(Locale.ENGLISH);
}
}
});
// Start button click listener
sbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (!isRunning) {
// Show repeat button, hide start and show pause
rbtn.setVisibility(View.VISIBLE);
sbtn.setText("Pause");
process();
isRunning = true;
} else {
sbtn.setText("Start");
rbtn.setVisibility(View.GONE);
isRunning = false;
}
}
});
// Repeat button click listener
rbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Repeat recently generated numbers
Utilities.speakNums(num[0], num[1], tts);
}
});
}
public void onPause() {
if (tts != null) {
tts.stop();
tts.shutdown();
}
super.onPause();
}
// Get input and compare with stored values, announce if user answer is correct or incorrect
public void submitAns() {
txt.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter/done" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER)) {
// Check to make sure the text field is not empty
if (TextUtils.isEmpty(txt.getText().toString())) {
Toast.makeText(MainActivity.this, "Enter a Number!", Toast.LENGTH_SHORT).show();
return false;
}
int intValue = Integer.parseInt(txt.getText().toString());
if (added == intValue) {
Toast.makeText(MainActivity.this, "Correct", Toast.LENGTH_SHORT).show();
tts.speak("Correct", TextToSpeech.QUEUE_ADD, null, null);
} else {
Toast.makeText(MainActivity.this, added + " is the Correct Answer!", Toast.LENGTH_SHORT).show();
tts.speak("Incorrect", TextToSpeech.QUEUE_FLUSH, null, null);
Utilities.speakAns(added, tts);
tts.speak("is the Correct answer", TextToSpeech.QUEUE_ADD, null, null);
}
txt.setText("");
return true;
}
return false;
}
});
}
public void process() {
num = Utilities.askQuestion(minNum, maxNum, tts);
added = Utilities.add(num[0], num[1]);
Utilities.focus(txt, getApplicationContext());
submitAns();
}
}
UTILITIES.java
import android.content.Context;
import android.speech.tts.TextToSpeech;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import java.util.Random;
public class Utilities {
// Function to generate random numbers in range
public static int randomGen(int minNum, int maxNum) {
final Random randNum = new Random();
return randNum.nextInt(maxNum - minNum) + minNum;
}
public static int add(int num1, int num2) {
return num1 + num2;
}
public static int sub(int num1, int num2) {
return num1 - num2;
}
// Speak individual numbers with operator in between speech
public static void speakNums(int r1, int r2, TextToSpeech tts) {
String toSpeak = Integer.toString(r1);
String nexToSpeak = Integer.toString(r2);
tts.speak(toSpeak, TextToSpeech.QUEUE_ADD, null, null);
tts.speak("Plus", TextToSpeech.QUEUE_ADD, null, null);
tts.speak(nexToSpeak, TextToSpeech.QUEUE_ADD, null, null);
}
// Speak answer
public static void speakAns(int a, TextToSpeech tts) {
String sumSpeak = Integer.toString(a);
tts.speak(sumSpeak, TextToSpeech.QUEUE_ADD, null, null);
}
// Request focus so that keyboard pops up as generate button is tapped
public static void focus(EditText txt, Context context) {
txt.requestFocus();
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(txt, InputMethodManager.SHOW_IMPLICIT);
}
// Generate question, speak question, and return array of random numbers for other operations
public static int[] askQuestion(int minNum, int maxNum, TextToSpeech tts) {
int r1 = randomGen(minNum, maxNum);
int r2 = randomGen(minNum, maxNum);
speakNums(r1, r2, tts);
return new int[] { r1, r2};
}
}
If you want to have a repeating/infinite series of events that involve user interaction (like answering a question) you should set it up as a series of chained events rather than trying to use an actual code loop or handler thread.
An example of how this could be set up is:
Generate a question to show (e.g. "What is 12 + 54?")
When the user is done answering, call a "submit" method that checks their answer and either shows an error or generates a new question to show.
Repeat the cycle above for as long as you want. No loops or handler threads are needed for this.
In terms of architecture, separating out as much of the question-generation and answer-processing logic into a ViewModel will help you tremendously, then the activity can just observe the relevant state in the view model (like what question to show).
Here is a simple example based on the description and example code you provided. There is a ViewModel that handles creating a question, checking the answer, and advancing to a new question, and an Activity that observes the relevant state from the ViewModel. Try it out in a blank app project to understand how it works.
ViewModel
public class MainViewModel extends ViewModel {
// Error string to observe - to show an error message or toast
private final MutableLiveData<String> error = new MutableLiveData<>("");
LiveData<String> getError() {
return error;
}
// Current question to show
private final MutableLiveData<String> question = new MutableLiveData<>("");
LiveData<String> getQuestion() {
return question;
}
// Text to show on the start/pause button
private final MutableLiveData<String> startPauseButton = new MutableLiveData<>("START");
LiveData<String> getStartPauseButton() {
return startPauseButton;
}
// private internal state, e.g. current question,
// expected answer, play/pause state
private int expected = 0;
private String current_question = "";
private boolean playing = false;
private final Random random = new Random();
private final int minNum = 10;
private final int maxNum = 100;
private int getNumber() {
return random.nextInt(maxNum - minNum) + minNum;
}
// Process a user's answer, and either show an error
// message or generate a new question to show
void submitAnswer(String ans) {
try {
int a = Integer.parseInt(ans);
if( a == expected ) {
generateNewQuestion();
question.postValue(current_question);
}
else {
error.postValue("Incorrect answer, try again");
}
}
catch (NumberFormatException e) {
error.postValue("Not a number - enter a number");
}
}
private void generateNewQuestion() {
int a = getNumber();
int b = getNumber();
expected = a + b;
current_question = "What is " + a + " + " + b + "?";
}
void clearError() {
error.postValue("");
}
// Called when the user clicks the play/pause button
void clickStartPause() {
playing = !playing;
if( playing ) {
startPauseButton.postValue("PAUSE");
question.postValue(current_question);
}
else {
startPauseButton.postValue("START");
question.postValue("");
}
}
public MainViewModel() {
generateNewQuestion();
}
}
Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView question = findViewById(R.id.question);
EditText answer = findViewById(R.id.answer);
Button start = findViewById(R.id.start);
Button submit = findViewById(R.id.submit);
question.setVisibility(View.GONE);
answer.setVisibility(View.GONE);
submit.setVisibility(View.GONE);
MainViewModel model = new ViewModelProvider(this).get(MainViewModel.class);
// Observe the current question, and if it is blank
// hide the question/answer/submit views
final Observer<String> questionObserver = questionTxt -> {
if( questionTxt == null || questionTxt.isEmpty() ) {
question.setVisibility(View.GONE);
answer.setVisibility(View.GONE);
submit.setVisibility(View.GONE);
}
else {
question.setVisibility(View.VISIBLE);
answer.setVisibility(View.VISIBLE);
submit.setVisibility(View.VISIBLE);
question.setText(questionTxt);
}
};
// Observe the error state, if it is non-blank show
// a toast then reset the state (so the toast only shows once)
final Observer<String> errorObserver = errorText -> {
if( errorText != null && !errorText.isEmpty() ) {
Toast.makeText(this, errorText, Toast.LENGTH_LONG).show();
model.clearError();
}
};
model.getError().observe(this, errorObserver);
model.getQuestion().observe(this, questionObserver);
model.getStartPauseButton().observe(this, start::setText);
submit.setOnClickListener(v -> {
model.submitAnswer(answer.getText().toString());
answer.setText("");
});
start.setOnClickListener(v -> model.clickStartPause());
}
XML
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/question"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<EditText
android:id="#+id/answer"
android:hint="Answer"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/question"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/answer"/>
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/answer"/>
</androidx.constraintlayout.widget.ConstraintLayout>
I am working through a Udemy course and we're building a basic "Higher or Lower" app. My app essentially works, however the random number it chooses for us to guess is always the same no matter how many times I destroy and relaunch the activity.
My MainActivity.java:
//mad import statements here
public class MainActivity extends AppCompatActivity {
int correctNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int correctNumber = generateNum();
}
protected int generateNum(){
Random rand = new Random();
int randNum = rand.nextInt(100);
return randNum;
}
protected void numberEval(View view) {
EditText enteredNumber = (EditText) findViewById(R.id.numberEntry);
String numberString = enteredNumber.getText().toString();
Button pressMe = (Button) findViewById(R.id.button);
int numToEval = Integer.parseInt(numberString);
String result;
TextView showWinLose = (TextView) findViewById(R.id.winLoseText);
if (numToEval > correctNumber) {
result = "Too high!";
} else if (numToEval < correctNumber) {
result = "Too Low!";
}else {
result = "You guessed it!";
}
showWinLose.setText(result);
}
}
Super super basic, yes? Originally, my numberEval() method called generateNum(), but then I realized it was generating a new number to guess every time I pressed the button. So I set it the way it was here, where onCreate() generates correctNumber only once and correctNumber is now a class variable. Now it doesn't generate a new number every button click, but it won't seem to generate a new number at all. It's stuck at 0 no matter how any times I launch, close, relaunch, etc. the app.
How can I fix this? Thanks in advance.
public class MainActivity extends AppCompatActivity {
int correctNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int correctNumber = generateNum();
}
// ...
}
The last line in onCreate() declares a local variable named correctNumber. This hides the class field with the same name and is only available inside onCreate(). To fix the problem, remove int from this line so that you use the class field instead.
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
package com.elitiv.calculatorvamalv2;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
private EditText editYear;
private EditText editCapacity;
private EditText Result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Year = (TextView)findViewById(R.id.Year);
TextView Capacity = (TextView)findViewById(R.id.Capacity);
editYear = (EditText)findViewById(R.id.editYear);
editCapacity = (EditText)findViewById(R.id.editCapacity);
Result = (EditText)findViewById(R.id.Result);
Button calc =(Button)findViewById(R.id.calc);
final RadioButton RadioD = (RadioButton)findViewById(R.id.radioD);
final RadioButton RadioB = (RadioButton)findViewById(R.id.radioB);
calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double editYear = editYear.getText().toString();
double editCapacity = editYear.getText().toString();
if(RadioB.isChecked()){
switch(editYear){
case 2014:
if(editCapacity >0 && NCapacity <1000){
Result = editCapacity*0.57;
}
if(editCapacity >1000 && editCapacity <1500){
Result = editCapacity*0.67;
}
if(editCapacity >1501 && editCapacity <2000){
Result = editCapacity*1.00;
}
break;
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
});
}
}
I'm trying to make a simple app that is adding 2 values by a condition, itdidn't compile the app yet because of 2 errors:
Syntax error on token ")", { expected MainActivity.java /CalculatorVamalV2/src/com/elitiv/calculatorvamalv2 line 65 Java Problem
Syntax error, insert "}" to complete ClassBody MainActivity.java /CalculatorVamalV2/src/com/elitiv/calculatorvamalv2 line 86 Java Problem
When android studio gives me a problem i switch to eclipse and viceversa.
The error messages are very descriptive. Check file MainActivity.java, line 65 and fix it. Similar for line 86.
Anyway, these are the errors:
Syntax error on token ")", { expected MainActivity.java /CalculatorVamalV2/src/com/elitiv/calculatorvamalv2 line 65 Java Problem
You missed a close brace } in the inner class definition:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Year = (TextView)findViewById(R.id.Year);
TextView Capacity = (TextView)findViewById(R.id.Capacity);
editYear = (EditText)findViewById(R.id.editYear);
editCapacity = (EditText)findViewById(R.id.editCapacity);
Result = (EditText)findViewById(R.id.Result);
Button calc =(Button)findViewById(R.id.calc);
final RadioButton RadioD = (RadioButton)findViewById(R.id.radioD);
final RadioButton RadioB = (RadioButton)findViewById(R.id.radioB);
calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double editYear = editYear.getText().toString();
double editCapacity = editYear.getText().toString();
if(RadioB.isChecked()){
switch(editYear){
case 2014:
if(editCapacity >0 && NCapacity <1000){
Result = editCapacity*0.57;
}
if(editCapacity >1000 && editCapacity <1500){
Result = editCapacity*0.67;
}
if(editCapacity >1501 && editCapacity <2000){
Result = editCapacity*1.00;
}
break;
}
}
} // <-- add this here
});
}
Syntax error, insert "}" to complete ClassBody MainActivity.java /CalculatorVamalV2/src/com/elitiv/calculatorvamalv2 line 86 Java Problem
You don't have an anonymous class here, so there's no need to have );:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
Besides this, you have an additional brace } at the end of the definition of your class.
How to avoid these problems?
Indent your code. Since you're using eclipse and android development studio (based on Eclipse), you have this shortcut to let the IDE auto format the code automatically for you: Ctrl + Shift + F.
There's a quick and easy way to identify } ) and ; errors in Eclipse. I can see your spacing and indentation is off and not standardized.
While in Eclipse, highlight your entire class, or press ctrl-a.
Then hold down shift-ctrl and press f. This automatically formats your code and indents properly. This should make it easy to find out where you are missing the }. I know this works in the Windows build of Eclipse.
I can't for the life of me get this do while loop running on a button click even with a runnable and handler. Can anyone please show me where I have made my mistake?
{//Class start
Button roll;
int numberOfDice;
int diceCounter;
EditText diceBox;
EditText outputBox;
String diceNum;
Handler myHandler = new Handler();
private boolean Running;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_roller_screen);
roll = (Button) findViewById(R.id.roll_Button);
diceBox = (EditText) findViewById(R.id.numberOfDiceBox);
outputBox = (EditText) findViewById(R.id.outputBox);
roll.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
myHandler.post(runner);
}
});
}
Runnable runner = new Runnable() {
int dicecounter = 1;
Random rand = new Random();
public void run() {
do{
int dice_Value = rand.nextInt((6 - 1) + 1) - 1;
diceNum = diceBox.getText().toString();
numberOfDice = Integer.parseInt(diceNum);
outputBox.setText("Dice"+dicecounter+dice_Value);
dicecounter++;
}while(diceCounter <= numberOfDice);
myHandler.post(this);
}
};
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.roller_screen, menu);
return true;
}
}
I can't figure out if it's my logic or if I'm using runners and handlers completely wrong.
This is almost certainly an endless loop problem: dicecounter starts at one so if you enter a value less than two into diceBox the do..while loop will never terminate. The counter should start at zero and you should validate the number of dice value entered by the user (including checking for number format exception, unless the edit control only allows digits?)
But in any case, why is there a do..while loop there anyway? All it does is overwrite the text in outputBox, so even if the loop was fixed all the user will see is the result of the last iteration of the loop.
My slot machine is still in progress. I am trying to get a method from one class to another but I can't figure it out. Could anyone please help me? Here is my first code which I wanted to call the method from the other class:
GameMainActivity:
package com.ics136leeward.slotmachine;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ViewFlipper;
public class GameMainActivity extends Activity {
ViewFlipper slotOne, slotTwo, slotThree, spinStop;
Button spin, stop, bet;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game_main);
this.initSpin();
this.initStop();
}
private void initSpin() { //initialize Spin Method
spin = (Button) findViewById (R.id.spinBtn);
slotOne = (ViewFlipper) findViewById (R.id.slot1);
slotTwo = (ViewFlipper) findViewById (R.id.slot2);
slotThree = (ViewFlipper) findViewById (R.id.slot3);
spinStop = (ViewFlipper) findViewById (R.id.spinstopbutton);
spin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
slotOne.setFlipInterval(40);
slotOne.startFlipping(); //slot 1 spin
slotTwo.setFlipInterval(50);
slotTwo.startFlipping(); //slot 2 spin
slotThree.setFlipInterval(60);
slotThree.startFlipping(); //slot 3 spin
spinStop.showNext(); // shows the stop button
}
});
}
private void initStop() { //initialize Stop Method
stop = (Button) findViewById (R.id.stopBtn);
stop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
slotOne.stopFlipping(); //stops slot 1
slotTwo.stopFlipping(); //stops slot 2
slotThree.stopFlipping(); //stops slot 3
spinStop.showNext(); //shows the spin button again
if(slotOne == slotTwo || slotTwo == slotThree) {
}
}
});
}
}
Here is the second java class which I wanted to call the method getBet1() and getBet5() to the first activity:
Bet:
package com.ics136leeward.slotmachine;
import android.app.Activity;
import android.widget.TextView;
public class Bet extends Activity {
TextView userBet, bankRoll, event;
final int BETONE = 1, BETFIVE = 5;
int uBet = 100, bet;
public void getBet1() {
userBet = (TextView) findViewById (R.id.userBet);
bankRoll = (TextView) findViewById (R.id.bankroll);
uBet -= BETONE;
bet += BETONE;
userBet.setText("Your Bet: " + bet);
bankRoll.setText("" + uBet);
return;
}
public void getBet5() {
userBet = (TextView) findViewById (R.id.userBet);
bankRoll = (TextView) findViewById (R.id.bankroll);
uBet -= BETFIVE;
bet += BETFIVE;
userBet.setText("Your Bet: " + bet);
bankRoll.setText("" + uBet);
return;
}
}
You need to make a Utility class not a Activity class
So change
public class Bet extends Activity {
to
public class Bet // Normal java class
Since its not a Activity class there is not need to initialize views
userBet = (TextView) findViewById (R.id.userBet); //remove them
// Initialize all yours views in Activity
Now in Activity class
Bet bet = new Bet();
int value =bet.getBet1();
In getBet1() do you calculations an return values.
Then in Activity you can set the value to TextView
textView.setText(String.valueOf(value));
Do also check raghav's answer #
Can i Create the object of a activity in other class?
you can define those method static and then call by its class name like
Bet.getBet1();
Bet.getBet5();
or simply you can create class object and then call it
Bet b = new Bet();
b.getBet1()
Note: You don't need to extend to Activity class as you are not using any UI. (Already suggested by Raghunandan)
Try this code in the onCreate method of your calling class i.e. GameMainActivity
Bet bet= new Bet(); // where bet is object of Bet Class
bet.getBet1();
bet.getBet5();
However, You can create the object of Bet class in any method and access the class methods provided their access modifier must be public.