views:

69

answers:

3

I have created a custom class for my Android project called "Sounds" I want to be able to call it from my activity. The contents of my class are as follows:

package com.mypackage;

import java.util.HashMap;

import android.content.Context;
import android.media.SoundPool;

public class Sounds {

private static boolean sound = true;

private static final int FLIP_SOUND = 1;

private static Context context;
private static SoundPool soundPool;
private static HashMap<Integer, Integer> soundPoolMap;

public static void initSounds() {
    soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));
}

public static void playFlip() {
        soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1);
}

public static void setSound(Boolean onOff) {
    sound = onOff;
}
}

In my main Activity class I have tried importing the class, creating an instance of it but I guess I'm just not understanding how it's done. Can anybody point me in the right direction please?

A: 

Try

Sounds s = new Sounds();
s.initSounds();
s.playFlip();
s.setSound(true);
Shane Oliver
+1  A: 

Edited: From your Activity class:

private Sounds s;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);        
        s = new Sounds(this);
        s.initSounds();
}

You might also send the context with the constructor to your custom class.

Remove the static variables and methods:

public class Sounds {

private boolean sound = true;

private int FLIP_SOUND = 1;

private Context context;
private SoundPool soundPool;
private HashMap soundPoolMap;

public Sounds(Context context){
   this.context = context;
   soundPoolMap = new HashMap();
   soundPool = new SoundPool(0, AudioManager.STREAM_MUSIC, 0);
}

public void initSounds() {
   soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));
}

public void playFlip() {
    soundPool.play(soundPoolMap.get(FLIP_SOUND), 1, 1, 1, 0, 1);
}

public void setSound(Boolean onOff) {
   sound = onOff;
}
}
Charlie Sheen
I have tried everything specified here with my last resort being the "static"'s being the cause, after the suggestion up top I removed them where told and the program still forcecloses as soon as the activity attempts to call s.initSounds(); I'm assuming the problem is the context, as it gave me problems before. Can you suggest how I should pass the context correctly?
Hamid
@Hamid: I have edited my answer.
Charlie Sheen
Thanks, I have done this but the program still forcecloses when i call initSounds(); If I comment this out it works fine. I'm not sure if it's relevent, but the activity that I'm working in is called from a previous (parent?) activity by an intent...
Hamid
@Hamid: Start DDMS and look for an error message when this force close takes place.
Charlie Sheen
I have been using logcat to do the same thing, it shows: FATAL EXCEPTION main, RuntimeException "Unable to start Activity" com.mypackage.MyActivity, followed by java.lang.NullPointerException. Caused by NullPointerException at com.mypackage.Sounds.initSounds(Sounds.java:21) at com.mypackage.MyActivity.onCreate(MyActivity.java:42)....at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1069) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2751)
Hamid
That sounds strange... I'm not familiar with the `SoundPool` class, but are you sure that this line is correct: `soundPoolMap.put(FLIP_SOUND, soundPool.load(context, R.raw.flip, 1));`?
Charlie Sheen
Seems pretty obvious to me at this point, you never initialized `soundPoolMap`, so it's a null reference. Make sure to change it to `private HashMap<Integer, Integer> soundPoolMap = new HashMap<Integer, Integer>();` (I think)
Andrew Koester
@Andrew: Initializing made no difference. @Charlie: That line is the problem, commenting it out stops the forceclose, as far as I know though it is correct. Again, I believe the context might be the problem as it gave me problems before and that's why I shifted all this into a seperate class.
Hamid
@Hamid: You have to initialize it like Andrew wrote earlier. Try change my updated code.
Charlie Sheen
Well, it probably did make a difference, it likely moved the NullPointerException from soundPoolMap to something else. Are you initializing `soundPool` too, like Charlie's example? Are you passing the reference to `context` too?
Andrew Koester
That worked perfectly, I had already tried this before but it just force closed, it seems your parameters to the new soundPool instance were the key difference, I had SoundPool(5,AudioManager.STREAM_MUSIC,1); the first number apparently is how many simultanious stream are allowed, I had it at 5, why does zero work? and the last 0 is the priority, the API reference says it should be kept at 1 for future comptability because it isnt yet implemented.
Hamid
Thank you both Andrew and Charlie for your fast and conclusive help that has solved the problem. Now I need to go take something for my headache before I continue. Thanks again.
Hamid
@Hamid: Glad we could help you!
Charlie Sheen
A: 

You have made all the methods of the class static. If you want to use them as-is, call them with Sounds.initSound() and so on. However, since you have class variables, static methods and variables don't look appropriate. Remove static from your members (except from FLIP_SOUND) and then try creating an instance of the class and calling methods like normal.

Andrew Koester