gpt4 book ai didi

java - 将声音加载到 soundpool 时出现空指针错误

转载 作者:行者123 更新时间:2023-12-01 13:41:07 27 4
gpt4 key购买 nike

我做错了什么?任何帮助将不胜感激日志显示调用了 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");

}


}

堆栈跟踪(我想就是这样) -------------------------------------- ----------------------------------

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

最佳答案

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

您可以从 soundEffects 类中删除“extends Activity”,而是替换您的

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

并从您的主 Activity 传递一个引用,该引用似乎是您的 Display 类,一直到 gun.reload(..),它应该是 gun.reload(上下文上下文).

关于java - 将声音加载到 soundpool 时出现空指针错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20765773/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com