2017-05-05 4 views
0

私のクラスの別のクラスのSoundManagerクラスからメソッドを呼び出す際に問題があります。ここにSoundManagerクラスがあります。異なるクラスのサウンドクラスを再生する

パブリッククラスSoundManager {

private SoundPool soundPool; 
public static final int maxSounds = 2; 
AudioManager audioManager; 
private int[] soundId; 
private Context context; //= mGame.getActivity().getApplicationContext(); 
Game mGame; 
public SoundManager(Game game) { 
    super(); 
    context = game.getActivity().getApplicationContext(); 
    //this.mGame = game; 

    soundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0); 

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { 
     soundPool = new SoundPool.Builder() 
       .setMaxStreams(maxSounds) 
       .build(); 
    } else { 
     soundPool = new SoundPool(maxSounds, AudioManager.STREAM_MUSIC, 0); 
    } 

    soundId = new int[16]; 

    soundId[0] = soundPool.load(context, R.raw.sound1, 1); 
    //soundId[1] = soundPool.load(context, R.raw.cardsound, 1); 
    soundId[2] = soundPool.load(context, R.raw.swoosh, 1); 
    soundId[3] = soundPool.load(context, R.raw.clicksound, 1); 
    soundId[4] = soundPool.load(context, R.raw.scifi, 1); 
    soundId[5] = soundPool.load(context, R.raw.phasebattle, 1); 
    soundId[6] = soundPool.load(context, R.raw.card_mr_electron, 1); 
    soundId[7] = soundPool.load(context, R.raw.card_james_watt, 1); 
    soundId[8] = soundPool.load(context, R.raw.card_marvin, 1); 
    soundId[9] = soundPool.load(context, R.raw.card_leonardo, 1); 
    soundId[10] = soundPool.load(context, R.raw.card_edmund, 1); 
    soundId[11] = soundPool.load(context, R.raw.card_terminator, 1); 
    soundId[12] = soundPool.load(context, R.raw.card_mr_robot, 1); 
    soundId[13] = soundPool.load(context, R.raw.card_labrat, 1); 
    soundId[14] = soundPool.load(context, R.raw.card_nerd, 1); 
    soundId[15] = soundPool.load(context, R.raw.card_angle, 1); 

    audioManager = (AudioManager) context.getSystemService(Context.AUDIO_SERVICE); 
} 

public void playSound(int sound, float volume) { 
    soundPool.play(soundId[sound], volume, volume, 1, 0, 1f); 
} 

そして、ここでは、私はそれを呼び出すようにしようとしています第二のクラスの一部です。私がアプリケーションを実行するために行くと、スタックトレースは、私がnullのオブジェクトリファレンスで.getActivity()を初期化することができないと言って、コンテキストを初期化するSoundManagerの行に私を戻します。

パブリッククラスプレーヤー{

private GameScreen mGameScreen; 

private Game mGame; 
SoundManager mSoundManager = new SoundManager(mGame); 

public Player(GameScreen gameScreen, GameEventHandler mGameEventHandler, Game game) { 
    super(); 

} 

答えて

0

あなたがSoundManagerを初期化し、Gameはまだ存在していません。

はこれを試してみてください:

public class Player { 

private GameScreen mGameScreen; 

    private Game mGame; 
    SoundManager mSoundManager; 

    public Player(GameScreen gameScreen, GameEventHandler mGameEventHandler, Game game) { 
      super(); 
      mSoundManager = new SoundManager(game) 

    } 
    . 
    . 
+0

あなたは絶対的なライフセーバーがそんなにあなたに感謝だ!!!! –

+0

:)喜んで助けてください。それがあなたのために働いた場合、答えを受け入れてください。 – rupps

関連する問題