我根据用户选择的RadioButton创建了一个活动。我不知道为什么我的代码不起作用。这是我的非工作代码。(我想在用户检查radiobutton时更改背景颜色)
public class RegisterActivity extends Activity {
/** Called when the activity is first created. */
RadioButton rbRed;
RadioButton rbBlue;
TexView header;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.cafechoice);
rbRed = (RadioButton) findViewById(R.id.radioRed);
rbBlue = (RadioButton) findViewById(R.id.radioBlue);
header = (TextView) findViewById(R.id.textview);
rbRed.setOnCheckChangeListener(radio_listener);
rbBlue.setOnCheckChangeListener(radio_listener);
}
final CompoundButton.OnCheckedChangeListener radio_listener = new CompoundButton.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
if (!isChecked)
return;
if (buttonView.getId() == R.id.radioRed)
{
header.setBackgroundColor(Color.parseColor("#E23DA4"));
}
else if (buttonView.getId() == R.id.radioBlue)
{
header.setBackgroundColor(Color.parseColor("#548BF7"));
}
}
};
logcat出现错误
rbRed.setOnCheckChangeListener(radio_listener);
由java.lang.nullpointerexception引起