我正在写一个基本的音板。我已经把它放在我的手机上,它工作正常。大多数情况下都很好。前 7 个按钮有效,但第 8 和第 9 个按钮无效。我唯一的想法是这是一个内存问题,我不应该在程序开始时同时创建所有 9 个 mp。也许第 8 和第 9 的记忆有点太多了。
有人对为什么第 8 个和第 9 个按钮在以下代码中不起作用有任何想法吗?他们在模拟器中工作,但让我的手机崩溃。我知道代码并不完美 - 我正在学习。但是朝着正确的方向前进会很棒。
谢谢你尽你所能的帮助!
package testing.Variables.Levack;
import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.view.View;
import android.media.MediaPlayer;
public class TestingVariablesActivity extends Activity {
/** Called when the activity is first created. */
TextView tt;
Button messageButton1,messageButton2,messageButton3,messageButton4,messageButton5,messageButton6,messageButton7,messageButton8,messageButton9;
MediaPlayer mp1,mp2,mp3,mp4,mp5,mp6,mp7,mp8,mp9 = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mp1 = MediaPlayer.create(this, R.raw.baboon);
mp2 = MediaPlayer.create(this, R.raw.backoff);
mp3 = MediaPlayer.create(this, R.raw.dark6);
mp4 = MediaPlayer.create(this, R.raw.dontneedroads);
mp5 = MediaPlayer.create(this, R.raw.hua);
mp6 = MediaPlayer.create(this, R.raw.hulk);
mp7 = MediaPlayer.create(this, R.raw.itsaclipon);
mp8 = MediaPlayer.create(this, R.raw.ht);
mp9 = MediaPlayer.create(this, R.raw.truth1);
messageButton1=(Button)findViewById(R.id.messageButton1);
messageButton1.setOnClickListener(startListener);
messageButton2=(Button)findViewById(R.id.messageButton2);
messageButton2.setOnClickListener(startListener);
messageButton3=(Button)findViewById(R.id.messageButton3);
messageButton3.setOnClickListener(startListener);
messageButton4=(Button)findViewById(R.id.messageButton4);
messageButton4.setOnClickListener(startListener);
messageButton5=(Button)findViewById(R.id.messageButton5);
messageButton5.setOnClickListener(startListener);
messageButton6=(Button)findViewById(R.id.messageButton6);
messageButton6.setOnClickListener(startListener);
messageButton7=(Button)findViewById(R.id.messageButton7);
messageButton7.setOnClickListener(startListener);
messageButton8=(Button)findViewById(R.id.messageButton8);
messageButton8.setOnClickListener(startListener);
//messageButton9=(Button)findViewById(R.id.messageButton9);
//messageButton9.setOnClickListener(startListener);
}
private OnClickListener startListener =new OnClickListener() {
public void onClick(View v)
{
switch(v.getId()) {
case R.id.messageButton1:
mp1.start();
break;
case R.id.messageButton2:
mp2.start();
break;
case R.id.messageButton3:
mp3.start();
break;
case R.id.messageButton4:
mp4.start();
break;
case R.id.messageButton5:
mp5.start();
break;
case R.id.messageButton6:
mp6.start();
break;
case R.id.messageButton7:
mp7.start();
break;
case R.id.messageButton8:
mp8.start();
break;
// case R.id.messageButton9:
// mp9.start();
//break;
}
}
};
}