您好我试图让3个单选按钮更改onclick侦听器的按钮,每个单选按钮在该按钮的单击侦听器上设置自己的按钮。 但我明白了 (MainActivity不是封闭类)
注意:radiobuttons处于设置活动状态,按钮处于主要活动状态。
设置
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);
}
});
}
});
}
}
更新
它现在给了我
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference whenever i clicked on the radio button
我试图改变,让用户从设置活动的任何解决方案中更改主要活动按钮的onclick监听器?
答案 0 :(得分:0)
您需要使用生活活动上下文作为new Intent
的第一个参数。
尝试在您的意图中将MainActivity.this
更改为Settings.this
,例如
Intent intent = new Intent(Settings.this, PC.class);
答案 1 :(得分:0)
写Settings.this
代替MainActivity.this
,如@howdoidothis所说。我认为你错误地使用了onClickListener
而不是onCheckChangedListener
。请参阅以下示例:
rd.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
Intent intent = new Intent(Settings.this, PC.class);
startActivity(intent);
}
});
答案 2 :(得分:0)
首先, 始终遵循可重用性,因为您使用了Radio组,并通过单击另一个按钮从单选按钮应用意图。
一旦使用下面的代码段,如果仍然无效,请在此处更新,
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);
}
}
});
答案 3 :(得分:0)
试用此代码
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (i == radioButtonpc.getId()) {
//your code
}
if (i == radioButtonxx.getId()) {
//your code
}
if (i == radioButton.getId()) {
//your code
}
if (i == radioButtonab.getId()) {
//your code
}
}
});