Grenade Grenade - 2 months ago 8
Android Question

Error at opening another Activity?

I'm simply trying to open another

Activity
when I click to
btnFemale
Button
and
btnMale
Button
.Now
btnfemale
Button
is works well but
btnMale
Button
doesnt work.

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


Button BtnFemale = (Button)findViewById(R.id.btnFemale);
final Button BtnMale = (Button) findViewById(R.id.btnMale);

BtnFemale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
startActivity(intent);


}
});

BtnMale.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v){

Intent intent = new Intent(getApplicationContext(),ActivityThree.class);
startActivity(intent);
}
});
}
}

Answer

create seperate listener, not one inside another

BtnFemale.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent(getApplicationContext(),ActivityTwo.class);
        startActivity(intent);

    }
});

BtnMale.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v){

        Intent intent = new Intent(getApplicationContext(),ActivityThree.class);
        startActivity(intent);
    }
});

You are adding listener of BtnMale when clicking on BtnFemale.