Question

what am I doing wrong? any help would be greatly apreciated the log shows the initAudio method called and sound pool created then crashes

 public class soundEffects extends Activity{
    SoundPool sp;
    int shot = 0;
    public void InitializeAudio(){
    Log.d("TEST", "initAudio method called");
       sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
       Log.d("TEST", "sound pool created sp= " + sp);
       shot = sp.load(this, R.raw.gunshot, 1);
       Log.d("TEST", "Audio is loaded :)");
    }

    public void fireSound(){
        Log.d("TEST", "fire sound method works");
        sp.play(shot, 1, 1, 0,0, 1);
        Log.d("TEST", "fire sound command sucessful");

    }


    }

Stack Trace(I think this is it) -----------------------------------------------------------

12-24 11:31:43.074: E/AndroidRuntime(14617): FATAL EXCEPTION: main
12-24 11:31:43.074: E/AndroidRuntime(14617): Process: com.example.firedatgun_v2, PID: 14617
12-24 11:31:43.074: E/AndroidRuntime(14617): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.firedatgun_v2/com.example.firedatgun_v2.Display}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2232)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2282)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.ActivityThread.access$800(ActivityThread.java:136)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1225)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.os.Handler.dispatchMessage(Handler.java:102)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.os.Looper.loop(Looper.java:136)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.ActivityThread.main(ActivityThread.java:5186)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at java.lang.reflect.Method.invoke(Native Method)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1015)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
12-24 11:31:43.074: E/AndroidRuntime(14617): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.media.SoundPool$SoundPoolImpl.load(SoundPool.java:491)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.media.SoundPool.load(SoundPool.java:159)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at com.example.firedatgun_v2.soundEffects.InitializeAudio(soundEffects.java:15)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at com.example.firedatgun_v2.gun.reload(gun.java:32)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at com.example.firedatgun_v2.Display.onCreate(Display.java:26)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.Activity.performCreate(Activity.java:5231)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1098)
12-24 11:31:43.074: E/AndroidRuntime(14617):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2196)
12-24 11:31:43.074: E/AndroidRuntime(14617):    ... 9 more
Was it helpful?

Solution

You're referencing an invalid Activity, in this case the soundEffect class. Just extending the Activity class won't give you all the pleasures of an Activity unless that Activity is explicitly called with onCreate(...) to set-up resources, etc.

You can remove the "extends Activity" from you soundEffects class, and instead, replace your

public void InitializeAudio()
{
   Log.d("TEST", "initAudio method called");
   sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
   Log.d("TEST", "sound pool created sp= " + sp);
   shot = sp.load(this, R.raw.gunshot, 1);
   Log.d("TEST", "Audio is loaded :)");
}

with

public void InitializeAudio(Context context)
{
   Log.d("TEST", "initAudio method called");
   sp = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
   Log.d("TEST", "sound pool created sp= " + sp);
   shot = sp.load(context, R.raw.gunshot, 1);
   Log.d("TEST", "Audio is loaded :)");
}

And pass a reference from you main Activity, which would appear to be your Display class, up through gun.reload(..), which should be gun.reload(Context context).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top