我有這樣的代碼:按鈕點擊收聽
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();
}
}
我有說,按鈕和看法是不正確的警告。
但是,我不明白上面的代碼有什麼問題。
看來我需要實例化按鈕類和View類。
但我不知道該怎麼做。
問題解決了嗎? – Raghunandan