Aruna Aruna - 3 months ago 9
Android Question

Changing one activity to another using RadioButton

I want to change one activity to another activity. It worked at the first time, But not at the next, How do I fix that?

public class Activity1 extends Activity {

RadioGroup radioGroup;
RadioButton Rd1, Rd2;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_activity1);

radioGroup=(RadioGroup)findViewById(R.id.radioGroup);
Rd1=(RadioButton)findViewById(R.id.radioButton);
Rd2=(RadioButton)findViewById(R.id.radioButton2);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(Rd1.isChecked())
{
Intent intent = new Intent(getApplicationContext(), Acitivity.class);
startActivity(intent);

}
else {
if (Rd2.isChecked()) {
Intent intent1 = new Intent(getApplicationContext(), Acivity1.class);
startActivity(intent1);

}
}
}
});
}
}

Answer

I noticed that you wanted to go from Activity1 to Activity.class, you cannot do that. Change Activity.class to existing activity, ex: Activity2.class. And you need to simplify your listener: to something like this:

@Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {
            Intent intent = null;
            if (Rd1.isChecked()) {
                intent = new Intent(Activity1.this, Activity2.class);
            } else if(Rd2.isChecked()) {
                intent = new Intent(Activity1.this, Activity3.class);
            }
            startActivity(intent);
        }
Comments