Reputation: 96
I am calling activity from single button clicked. I am putting my code here. So problem is when I am calling my rules activity on rules button clicked it calls it, but it also calls on other option button too.
public class Main extends Activity implements OnClickListener{
/** the button that I've added in the xml*/
Button options_btn;
Button Exit_label;
Button player_data_btn;
@Override
protected void onCreate(Bundle savedInstanceState) {
/** Called when the activity is first created. */
super.onCreate(savedInstanceState);
setContentView(R.layout.cyk_main);
/** Get the id of the button from the xml*/
options_btn = (Button) findViewById(R.id.options_btn);
options_btn.setOnClickListener(this);
/** Get the id of the button from the xml*/
player_data_btn = (Button) findViewById(R.id.player_data_btn);
player_data_btn.setOnClickListener(this);
}
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
/** make a refernce to store the intent when the view has been clicked*/
Intent intent;
Intent intent1;
/** Make cases according to the number of buttons you have in screen
* In this case, I've added one.*/
switch(view.getId()){
case R.id.options_btn :
Log.i("btnClick","Start button id is"+view.getId());
/** Intent should be fired from this activity to the other activity*/
intent = new Intent(Main.this, SoundLayout.class);
/** This is useful when you want to get the button that is clicked here on the
* destination activity*/
intent.putExtra("button_click", "Start");
/** Start the intent*/
startActivity(intent);
case R.id.player_data_btn :
Log.i("btnClick","Start button id is"+view.getId());
/** Intent should be fired from this activity to the other activity*/
intent1 = new Intent(Main.this, RulesActivity.class);
intent1.putExtra("button_click", "Start");
/** Start the intent*/
startActivity(intent1);
/*** Use this only when you want to finish your current activity while opening an another one.
* if this is commented, then this activity will be running in the background.
***/
this.finish();
break;
}
Button Exit_label=(Button)findViewById(R.id.Exit_label);
Exit_label.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder=new AlertDialog.Builder(Main.this);
//Set a title
builder.setTitle("Exit?");
//Set a message
builder.setMessage("Want to Exit?");
builder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
//Displaying a toast message
Toast.makeText(getApplicationContext(), "Your Game Exit", Toast.LENGTH_LONG).show();
finish();
System.exit(0);
}
});
builder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
});
//Create the dialog
AlertDialog alertdialog=builder.create();
//show the alertdialog
alertdialog.show();
}
});
}
@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;
}
}
Here when I am calling my rules then it also come on the option button. So how can I solve this?
Upvotes: 0
Views: 117
Reputation: 7486
One must place a break statement at the end of every case
. You are missing the break statement
in the end of first case. So if the first case if true, once the first case is executed, the second case will also be executed due to the absence of break
statement at the end of first case. This is causing the problem.
switch(view.getId()){
case R.id.options_btn :
....
break; // you are missing this
case R.id.player_data_btn :
....
break;
}
Upvotes: 0
Reputation: 14590
You have not mentioned the break statement for the first case statement. add the break it will work..
Upvotes: 2