Issue
I'm beginner in android dev and i'm creating a pizza clicker game, just like cookie clicker. I created an activity for upgrades and for upgrading you need some amount of pizza like if you have 10 pizzas you can upgrade. If the amount of pizzas is equals the price the button is enabled, if not, the button is not enabled. When I click the button, the amount of pizza is decreased and the button should disable again, but it's not disabling.
Here's the first activity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
public static int pizza = 0;
public static TextView pizzaContText, helpers;
public static Button add, upgrades, exit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
}
private void initialize() {
add = (Button) findViewById(R.id.makePizza);
exit = (Button) findViewById(R.id.exitButton);
upgrades = (Button) findViewById(R.id.upgrades);
pizzaContText = (TextView) findViewById(R.id.pizzas);
helpers = (TextView) findViewById(R.id.helpers);
pizzaContText.setText("Pizzas: " + pizza);
pizzaContText.setTextColor(Color.BLACK);
pizzaContText.setTextSize(40);
helpers.setText("Helpers: " + Upgrades.contHelper);
helpers.setTextSize(20);
helpers.setTextColor(Color.BLACK);
add.setOnClickListener(this);
upgrades.setOnClickListener(this);
exit.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.makePizza:
pizza++;
pizzaContText.setText("Pizzas: " + pizza);
pizzaContText.setTextColor(Color.BLACK);
pizzaContText.setTextSize(40);
break;
case R.id.upgrades:
Intent i = new Intent(getApplicationContext(), Upgrades.class);
startActivity(i);
break;
case R.id.exitButton:
finish();
System.exit(0);
break;
}
}
}
And here's the second activity (the upgrades):
public class Upgrades extends AppCompatActivity implements View.OnClickListener{
public static int contHelper = 0, priceHelper = 10;
Button addHelper, back;
Handler h = new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upgrades);
initialize();
if (MainActivity.pizza >= priceHelper){
//ENABLES THE BUTTON
addHelper.setEnabled(true);
} else{
//DISABLE THE BUTTON
addHelper.setEnabled(false);
}
}
private void initialize() {
addHelper = (Button) findViewById(R.id.addHelper);
addHelper.setText("Helper: " + priceHelper + " pizzas");
back = (Button) findViewById(R.id.back);
addHelper.setOnClickListener(this);
back.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addHelper:
MainActivity.pizza-=priceHelper;
addHelper.setText("Helper: " + priceHelper + " pizzas");
priceHelper+=4;
contHelper++;
//Auto clicks the make pizza button every 1 sec
final Runnable r = new Runnable() {
@Override
public void run() {
MainActivity.add.performClick();
h.postDelayed(this, 1000);
}
};
h.postDelayed(r, 1000);
break;
case R.id.back:
Intent i = new Intent(getApplicationContext(), MainActivity.class);
startActivity(i);
break;
}
}
}
Solution
Don't use static objects.. Is the worst thing you could do.. Use bundle to send your variable
Intent activity = new Intent(this, Upgrades.class);
activity.putExtra("pizza", pizza);
startActivity(intent);
And in your Upgrades activity use
Bundle extras = getIntent().getExtras();
int pizza = extras.getInt("pizza");
And check for nulls and that you send the correct things.
Answered By - Cătălin Florescu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.