I want to make a new button (again) appearing when one of my variables
reached the 5. I tried to do it with the if statements but It doesn't
make the button appearing, and I don't know exactly why.
Here is my code:
public class p extends Activity{
private static final int REQUEST_CODE = 1;
ImageView secret;
public int counter;
public int counter_foe;
public TextView txt;
public TextView counter_txt;
ImageView pic;
TextView counter_txt_foe;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.p);
final Button again = (Button) findViewById(R.id.again);
counter = 0;
counter_foe = 0;
pic = (ImageView) findViewById(R.id.pic);
final Button next = (Button) findViewById(R.id.next);
txt = (TextView) findViewById(R.id.txt);
secret = (ImageView) findViewById(R.id.secret);
counter_txt = (TextView) findViewById(R.id.counter_me);
counter_txt_foe = (TextView) findViewById(R.id.counter_me_foe);
counter_txt.setText(String.valueOf(counter));
counter_txt_foe.setText(String.valueOf(counter_foe));
final Runnable r1 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
int imageArr[] = new int[3];
imageArr[0] = R.drawable.r1_mirror;
imageArr[1] = R.drawable.s1_mirror;
imageArr[2] = R.drawable.p2_mirror;
Random rand = new Random();
int rndInt = rand.nextInt(3);
secret.setImageResource(imageArr[rndInt]);
if (rndInt == 0) {
Log.d("improve", "counter improve");
txt.setText("+1 for You");
counter += 1;
counter_txt.setText(String.valueOf(counter));
counter_txt_foe.setText(String.valueOf(counter_foe));
}
if (rndInt == 1) {
txt.setText("+1 for Me");
counter_foe += 1;
counter_txt.setText(String.valueOf(counter));
counter_txt_foe.setText(String.valueOf(counter_foe));
}
if (rndInt == 2) {
Log.d("improve", "draw");
counter_txt.setText(String.valueOf(counter));
counter_txt_foe.setText(String.valueOf(counter_foe));
txt.setText("Draw");
}
next.setVisibility(1);
}
};
again.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter = 0;
counter_foe = 0;
startActivity(new Intent("com.z.CHANCES"));
}
});
final Runnable r3 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
again.setVisibility(1);
}
};
final Runnable r4 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
next.setVisibility(-1);
}
};
if (counter == 5) {
again.post(r3);
next.post(r4);
}
else if (counter_foe == 5) {
again.post(r3);
next.post(r4);
}
final Runnable r2 = new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
next.setVisibility(-1);
}
};
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent("com.z.CHANCES");
i.putExtra("counter", counter);
i.putExtra("counter_foe", counter_foe);
startActivityForResult(i, REQUEST_CODE);
}
protected void onActivityResult(int requestCode, int resultCode,
Intent data) {
if (resultCode == RESULT_OK && requestCode ==
REQUEST_CODE) {
if (data.hasExtra("r")) {
pic.setBackgroundResource(R.drawable.r1);
}
else if (data.hasExtra("p")) {
pic.setBackgroundResource(R.drawable.p2);
}
else if (data.hasExtra("s")) {
pic.setBackgroundResource(R.drawable.s1);
}
}
}
});
secret.postDelayed(r1, 5000);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
SharedPreferences content = getSharedPreferences("counter", 0);
SharedPreferences.Editor editor = content.edit();
editor.putInt("counter", 0);
editor.putInt("counter_foe", 0);
editor.commit();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
SharedPreferences content = getSharedPreferences("counter", 0);
counter = content.getInt("counter", counter);
counter_foe = content.getInt("counter_foe", counter_foe);
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
SharedPreferences content = getSharedPreferences("counter", 0);
SharedPreferences.Editor editor = content.edit();
editor.putInt("counter", counter);
editor.putInt("counter_foe", counter_foe);
editor.commit();
}
Thanks for any helps. Sorry for this question, but It's very annoying,
that I everytime crash into something error.
--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en