0に達すると音を鳴らすカウントダウンタイマーを作ろうとしていますが、音を鳴らそうとすると強制的に閉じます。私のコードは正しいと確信していますが、なぜそれが閉じるのか理解できません。助けてください、なぜこの力は閉じるのですか?
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.Chronometer;
import android.widget.Chronometer.OnChronometerTickListener;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
Button start, stop;
int minCount, secCount;
Chronometer count;
boolean viberate, running;
SharedPreferences getPrefs;
MediaPlayer alarm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initilize();
count.start();
count.setOnChronometerTickListener(new OnChronometerTickListener() {
@Override
public void onChronometerTick(Chronometer chronometer) {
if (running) {
secCount--;
updateDisplay();
}
}
});
start.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
running = true;
alarm.release();
if (minCount <= 0 && secCount <= 0) {
nothing();
}
}
});
stop.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
running = false;
}
});
protected void done() {
// TODO
boolean music = getPrefs.getBoolean("Sound", true);
if (music && alarm != null) {
alarm.start(); //line 179 this is the line that causes the force close
}
running = false;
Toast.makeText(this, "Times Up!!!", Toast.LENGTH_LONG).show();
}
private void initilize() {
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
second = (TextView) findViewById(R.id.second);
minute = (TextView) findViewById(R.id.minute);
minCount = 1;
secCount = 0;
count = (Chronometer) findViewById(R.id.chronometer1); running = false;
getPrefs = PreferenceManager
.getDefaultSharedPreferences(getBaseContext());
alarm = MediaPlayer.create(MainActivity.this, R.raw.finished);
}
private void updateDisplay() {
if (minCount <= 0 && secCount <= 0) {
done();
}
if (secCount < 0) {
minCount--;
secCount = 59;
}
if (secCount < 0) {
secCount = 59;
second.setText("59");
} else if (secCount <= 9) {
second.setText("0" + secCount + "");
} else {
second.setText(secCount + "");
}
if (minCount < 0) {
minCount = 59;
minute.setText("59 :");
} else if (minCount <= 9) {
minute.setText("0" + minCount + " :");
} else {
minute.setText(minCount + " :");
}
}
@Override
protected void onStop() {
super.onStop();
alarm.release();
}
}
私のlogcat
12-12 19:55:50.423: E/AndroidRuntime(25573): FATAL EXCEPTION: main
12-12 19:55:50.423: E/AndroidRuntime(25573): java.lang.IllegalStateException
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.media.MediaPlayer._start(Native Method)
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.media.MediaPlayer.start(MediaPlayer.java:1025)
12-12 19:55:50.423: E/AndroidRuntime(25573): at com.example.timer.MainActivity.done(MainActivity.java:179)
12-12 19:55:50.423: E/AndroidRuntime(25573): at com.example.timer.MainActivity.updateDisplay(MainActivity.java:209)
12-12 19:55:50.423: E/AndroidRuntime(25573): at com.example.timer.MainActivity.access$0(MainActivity.java:207)
12-12 19:55:50.423: E/AndroidRuntime(25573): at com.example.timer.MainActivity$1.onChronometerTick(MainActivity.java:47)
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.widget.Chronometer.dispatchChronometerTick(Chronometer.java:279)
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.widget.Chronometer$1.handleMessage(Chronometer.java:271)
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.os.Handler.dispatchMessage(Handler.java:99)
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.os.Looper.loop(Looper.java:137)
12-12 19:55:50.423: E/AndroidRuntime(25573): at android.app.ActivityThread.main(ActivityThread.java:4978)
12-12 19:55:50.423: E/AndroidRuntime(25573): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 19:55:50.423: E/AndroidRuntime(25573): at java.lang.reflect.Method.invoke(Method.java:511)
12-12 19:55:50.423: E/AndroidRuntime(25573): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
12-12 19:55:50.423: E/AndroidRuntime(25573): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558)
12-12 19:55:50.423: E/AndroidRuntime(25573): at dalvik.system.NativeStart.main(Native Method)