将声音加载到soundpool时获取空指针错误

时间:2013-12-24 19:14:42

标签: java android soundpool

我做错了什么? 任何帮助都会大大减少 日志显示调用的initAudio方法,然后创建声音池崩溃

 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(我想是这样的)-------------------------------------- ---------------------

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

1 个答案:

答案 0 :(得分:1)

您引用的是无效的Activity,在本例中是soundEffect类。只是扩展Activity类不会给你一个Activity的所有乐趣,除非使用onCreate(...)显式调用Activity来设置资源等。

您可以从soundEffects课程中删除“扩展活动”,而是替换

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 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 :)");
}

并传递来自您的主要活动的引用,该活动似乎是您的Display课程,通过gun.reload(..),应该是gun.reload(Context context)