Tarek Zoubi Tarek Zoubi - 3 months ago 21
Android Question

Changing onclicklistener for a button from radio buttons

Hi im trying to make 3 radio buttons change onclick listener for a button each radiobutton sets its own on click listener for that button.
but i get
(MainActivity is not an enclosing class)

Note: the radiobuttons are in settings activity and the button is in mainactivity.

Settings

public class Settings extends MainActivity {

private RadioGroup radioGroup;
private RadioButton radioButtonpc;
private RadioButton radioButtonps;
private RadioButton radioButtonxb;
public Button settings;
public Button cheatsactivity;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);

cheatsactivity = (Button) findViewById(R.id.cheats_activity);
settings = (Button) findViewById(R.id.setting_btn);
radioGroup = (RadioGroup) findViewById(R.id.RadioGroup);
radioButtonpc = (RadioButton) findViewById(R.id.radioButton1);
radioButtonxb = (RadioButton) findViewById(R.id.radioButton2);
radioButtonps = (RadioButton) findViewById(R.id.radioButton3);

radioButtonpc.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cheatsactivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PC.class);
startActivity(intent);
}
});
}
});

radioButtonxb.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cheatsactivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, XBOX.class);
startActivity(intent);
}
});
}
});
radioButtonps.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
cheatsactivity.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, PS.class);
startActivity(intent);
}
});
}
});
}




}


Update
i tried all the answers
it gives me

java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
at android.view.View.performClick(View.java:5210)
at android.view.View$PerformClick.run(View.java:21294)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5530)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:21294) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5530) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.SparidApps.gta_v_cheats/com.example.user.gtav_cheats.Settings}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1801)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1514)
at android.app.Activity.startActivityForResult(Activity.java:3951)
at android.app.Activity.startActivityForResult(Activity.java:3912)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:842)
at android.app.Activity.startActivity(Activity.java:4235)
at android.app.Activity.startActivity(Activity.java:4203)
at com.example.user.gtav_cheats.MainActivity.opensettings(MainActivity.java:48)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
at android.view.View.performClick(View.java:5210) 
at android.view.View$PerformClick.run(View.java:21294) 
at android.os.Handler.handleCallback(Handler.java:739) 
at android.os.Handler.dispatchMessage(Handler.java:95) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5530) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:622) 
09-02 14:10:42.562 22787-22787/com.SparidApps.gta_v_cheats I/Process: Sending signal. PID: 22787 SIG: 9


Update 2
I declared the activity "Settings" in the manifest and it worked like a charm

Answer

First, always follow re-usability because you have used Radio group and you applying intent from Radio button with another button click.

Once go with below code snippet,if still not work then update here,

radioGroup 
        .setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
            Log.d("chk", "id" + checkedId);

                if (checkedId == R.id.radioButton1) {
                    //some code
                     Intent intent = new Intent(Settings .this, PC.class);
                    startActivity(intent);
                } else if (checkedId == R.id.radioButton2) {
                    //some code
                     Intent intent = new Intent(Settings .this, XBOX.class);
                    startActivity(intent);

                }else if (checkedId == R.id.radioButton3) {
                    //some code
                      Intent intent = new Intent(Settings .this, PS.class);
                    startActivity(intent);
                }

            }

        });