我對Java很陌生,所以如果我犯了一個非常愚蠢的錯誤,請耐心等待。 我試圖在使用SoundPool在計數器應用程序上單擊按鈕時播放聲音,但當按下任一按鈕時,該應用程序會崩潰。 編譯期間不顯示錯誤。 這是代碼。SoundPool崩潰應用程序
package com.example.c0unter;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.media.SoundPool;
import android.media.AudioManager;
public class MainActivity extends Activity {
private int Count=0;
private SoundPool spool;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ResetButton = (Button) findViewById(R.id.ResetButton);
Button CounterButton = (Button) findViewById(R.id.CounterButton);
final TextView CountText = (TextView) findViewById(R.id.CountText);
ResetButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Sound(R.raw.reset_button);
Count=0;
CountText.setText(" " + Count);
}
});
CounterButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Sound(R.raw.counter_button);
Count++;
CountText.setText(" " +Count);
}
});
}
public void Sound(int soundID){
spool.load(this, soundID ,1);
AudioManager audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
float volume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
spool.play(soundID, volume, volume, 1, 0, 1f);
};
}
你會爲崩潰粘貼logcat輸出嗎? – loeschg