I have this code:
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
MediaPlayer sound1, sound2;
sound1 = MediaPlayer.create(this, R.raw.cows);
sound2 = MediaPlayer.create(this, R.raw.sheep);
final Button button1 = (Button) findViewById(R.id.Button01);
button1.setOnClickListener(this);
final Button button2 = (Button) findViewById(R.id.Button02);
button1.setOnClickListener(this);
public void onClick(View v) {
switch(v.getId()) {
case R.id.Button01:
sound1.start();
break;
case R.id.Button02:
sound2.start();
break;
}
}
protected void onDestroy() {
sound1.release();
sound2.release();
super.onDestroy();
}
}
I have a warning that says that the button and the view are not correct.
However, I don't understand what's wrong with the above code.
It seems that I need to instantiate the button class and the View class.
But I don't know how to do that.