2015-05-06 88 views
0

我對我的代碼有問題,它應該更改我的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); 
}} 
+0

爲什麼錯誤是抱怨'com.example.clecks.reaction_game.OnCheckedChangeListener'?它是什麼?在這種情況下,什麼是'activity_settings'? – shkschneider

+0

activity_settings是我的設置活動 - 遊戲的菜單,我可以選擇背景和聲音。 mhmm ...我不知道,我刪除了com.example.clecks.reaction_game.OnCheckedChangeListener,現在我可以運行它,但有一個新的錯誤,我將更新... – Joker99

+0

意味着你有錯誤我幫你在另一個類com.example.clecks.reaction_game.OnCheckedChangeListener中修復。現在'activity_settings'是固定的。 – shkschneider

回答

0

CompoundButton本身是一個抽象類(擴展Button)。

您的activity_activity_settings佈局中不應該有CompoundButton。例如,可以使用Button來代替。

+0

我沒有任何複合按鈕在我的佈局中 - 它們都是單選按鈕 – Joker99

+0

然後在代碼中不*使用'CompoundButtons',而是使用'RadioButtons'代替。你爲什麼使用錯誤的類型? – shkschneider

+0

我有,但同樣的錯誤與(I'm就行了吳站在):changeToBlue.setOnCheckedChangeListener(新RadioButton.OnCheckedChangeListener(){ @覆蓋 公共無效onCheckedChanged(CompoundButton buttonView,布爾器isChecked){ changeToBlue(背景) ; } }); – Joker99