我對我的代碼有問題,它應該更改我的Start-Activity的背景顏色。我應該選擇藍色和紅色之間的單選按鈕(在Radiogroup中)。我總是得到這個錯誤:Android RadioButton CompoundButton.OnCheckedChangeListener錯誤
Error:(8, 8) error: com.example.clecks.reaction_game.OnCheckedChangeListener is not abstract and does not override abstract method onCheckedChanged(CompoundButton,boolean) in android.widget.CompoundButton.OnCheckedChangeListener C:\Users\clecks\Desktop\asdfasdf\app\src\main\java\com\example\clecks\reaction_game\OnCheckedChangeListener.java
當我點擊錯誤消息有打開 OnCheckedChangeListener.java
命名一個新的類:
public class OnCheckedChangeListener implements CompoundButton.OnCheckedChangeListener {}
這裏是我的代碼:
public class activity_settings extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity_settings);
colorchange();
}
public void colorchange() {
final RelativeLayout background = (RelativeLayout) findViewById(R.id.start);
final RadioButton changeToBlue = (RadioButton) findViewById(R.id.button_blue);
final RadioButton changeToRed = (RadioButton) findViewById(R.id.button_red);
final Button button_save = (Button) findViewById(R.id.button_save);
button_save.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
changeOption(background);
}
});
changeToBlue.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeToBlue(background);
}
});
changeToRed.setOnCheckedChangeListener(new RadioButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
changeToRed(background);
}
});
}
public void changeOption(RelativeLayout background) {
if (background.isEnabled()) {
background.setEnabled(false);
} else {
background.setEnabled(true);
}
}
public void changeToBlue(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
public void changeToRed(RelativeLayout background) {
background.setBackgroundColor(0x0000FF00);
background.invalidate();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_activity_settings, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}}
爲什麼錯誤是抱怨'com.example.clecks.reaction_game.OnCheckedChangeListener'?它是什麼?在這種情況下,什麼是'activity_settings'? – shkschneider
activity_settings是我的設置活動 - 遊戲的菜單,我可以選擇背景和聲音。 mhmm ...我不知道,我刪除了com.example.clecks.reaction_game.OnCheckedChangeListener,現在我可以運行它,但有一個新的錯誤,我將更新... – Joker99
意味着你有錯誤我幫你在另一個類com.example.clecks.reaction_game.OnCheckedChangeListener中修復。現在'activity_settings'是固定的。 – shkschneider