0

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;
}
}
1
  • Bet myBet = new Bet() ... myBet.getBet1()? Just noticed this is android. Not sure how that changes things.
    – Nico
    Commented Mar 28, 2014 at 6:06

3 Answers 3

1

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?

0
0

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()
0

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.

2
  • But Bet is a Activity class and creating an instance of Activity class is wrong. stackoverflow.com/questions/14956018/… Commented Mar 28, 2014 at 6:12
  • @Raghunandan As you have already suggested not to extend Activity. I have posted this in reference to that only. Commented Mar 28, 2014 at 6:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.