2017-05-05 1 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

관련 문제