I am new into Android programming. I am currently integrating media player in my app. Via the menu, I want the user to turn off the sound, but also start it again if desired. Right now I have a checkbox that is checked when the music is played and the music to be shut off when not checked.
I have managed to insert checkbox in the menu and got it to be checked when the app starts, just as it should. But when I try to uncheck it, the app shuts down ...
Anyone have publisher? See obvious errors? Or just want to give tips?
MainActivity
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
//Switch-sats i syfte om att det kommer tillkomma fler alternativ
//Switchen bygger på att hämta rätt id ifrån användarens val
switch (item.getItemId())
{
case R.id.action_help:
//Bytar Activity till help
Intent intent = new Intent(MainActivity1.this, help.class);
startActivity(intent);
return true;
case R.id.music:
final CheckBox music = (CheckBox)findViewById(R.id.music);
music.setChecked(true);
music.setOnClickListener(new OnClickListener(){
public void onClick(View v){
if (((CheckBox) v).isChecked()) {
backsound = MediaPlayer.create(MainActivity1.this, R.raw.backsound);
backsound.start();
backsound.setLooping(true);
}else{
music.setChecked(false);
backsound.stop();
}
}});
default:
return super.onOptionsItemSelected(item);
}}}
Main_activity.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/action_settings"/>
<item
android:id="@+id/action_help"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/help"/>
<item
android:id="@+id/music"
android:title="@string/musik"
android:checkable="true"
android:checked="true" />