0
我創建了一個小程序,當有人跳轉時播放馬里奧跳躍聲音。 我可以使用Z加速度,當程序達到10 m/s^2的某個值時,程序會顯示一條消息。我希望它從媒體播放器對象播放聲音,但是當我創建媒體播放器對象時,它會使代碼崩潰。我將在pastebin上發佈代碼。爲什麼創建Media Player對象會導致我的android代碼崩潰?
public class MainActivity extends Activity implements SensorEventListener {
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
MediaPlayer mp = MediaPlayer.create(getBaseContext(),R.raw.jump); //Line 17
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm= (SensorManager)getSystemService(SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration = (TextView) findViewById(R.id.acceleration);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
// TODO Auto-generated method stub
acceleration.setText("X: " + event.values[0] + "\nY: " + event.values[1] + "\nZ: " + event.values[2]);
if (event.values[2] > 10.8) {
acceleration.setText("Z IS REALLY BIG!!");
}
}
}
加入行17是什麼崩潰的程序...
謝謝!那真的很棒。 – Nathvi