我有一个Android应用程序,可以从服务器下载声音和图像来进行听力测试。但是,SoundPool有以下问题:
我已经把它剪回了最基本的部分,下面的代码复制了我的问题:
package com.grooble.moeigo;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.SparseIntArray;
import android.view.View;
import android.view.View.OnClickListener;
public class TestActivity extends Activity {
private String soundPath = "/storage/emulated/0/Music/questionSounds/";
private String[] soundWords = {"caravan.ogg", "clap.ogg", "cut.ogg", "hot.ogg", "knee.ogg"};
// private variables for SoundPool
private SoundPool soundPool;
SparseIntArray mySoundPoolMap = new SparseIntArray(5);
AudioManager audioManager;
boolean plays = false, loaded = false;
float actVolume, maxVolume, volume;
int counter;
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.test_activity);
// AudioManager audio settings for adjusting the volume
audioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
actVolume = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
maxVolume = (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
volume = actVolume / maxVolume;
//Hardware buttons setting to adjust the media sound
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
doSoundPool();
}
void doSoundPool(){
View soundButton = (View) findViewById(R.id.soundButton);
for (int i=0; i<soundWords.length; i++){
int loaded = soundPool.load(soundPath + soundWords[i], 1);
mySoundPoolMap.put(i, loaded);
}
//Set clickListeners for sounds to soundButton
soundButton.setOnClickListener(new MyOnClickListener(0){
});
}
class MyOnClickListener implements OnClickListener{
private int index;
public MyOnClickListener(int index){
super();
this.index = index;
}
public void onClick(View v) {
soundPool.play((int)mySoundPoolMap.get(index), 1.0f, 1.0f, 1, 0, 1.0f);
}
}
}
以下是LogCat输出,其价值如下:
12-22 23:46:25.544: W/UserFrag(6865): onCreateView: view not null
12-22 23:46:25.547: W/ListFrag(6865): onCreateView: view not null
12-22 23:46:25.554: D/OpenGLRenderer(6865): Render dirty regions requested: true
12-22 23:46:25.561: D/Atlas(6865): Validating map...
12-22 23:46:25.587: I/Adreno-EGL(6865): <qeglDrvAPI_eglInitialize:410>: QUALCOMM Build: 10/24/14, 167c270, I68fa98814b
12-22 23:46:25.588: I/OpenGLRenderer(6865): Initialized EGL, version 1.4
12-22 23:46:25.605: D/OpenGLRenderer(6865): Enabling debug mode 0
12-22 23:46:34.321: D/AndroidRuntime(6865): Shutting down VM
12-22 23:46:34.322: E/AndroidRuntime(6865): FATAL EXCEPTION: main
12-22 23:46:34.322: E/AndroidRuntime(6865): Process: com.grooble.moeigo, PID: 6865
12-22 23:46:34.322: E/AndroidRuntime(6865): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.grooble.moeigo/com.grooble.moeigo.TestActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.os.Handler.dispatchMessage(Handler.java:102)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.os.Looper.loop(Looper.java:135)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-22 23:46:34.322: E/AndroidRuntime(6865): at java.lang.reflect.Method.invoke(Native Method)
12-22 23:46:34.322: E/AndroidRuntime(6865): at java.lang.reflect.Method.invoke(Method.java:372)
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-22 23:46:34.322: E/AndroidRuntime(6865): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.grooble.moeigo.TestActivity.doSoundPool(TestActivity.java:48)
12-22 23:46:34.322: E/AndroidRuntime(6865): at com.grooble.moeigo.TestActivity.onCreate(TestActivity.java:40)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.Activity.performCreate(Activity.java:5933)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-22 23:46:34.322: E/AndroidRuntime(6865): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-22 23:46:34.322: E/AndroidRuntime(6865): ... 10 more
12-22 23:46:36.064: I/Process(6865): Sending signal. PID: 6865 SIG: 9
该错误似乎发生在第45行的负载上:
int loaded = soundPool.load(soundPath + soundWords[i], 1);
下载和保存似乎是工作的,实际上,在本例中,文件在运行上述示例之前位于文件夹中。
如果有人能看到这里发生了什么,并提供任何建议,这将是非常感谢的。
发布于 2014-12-22 15:08:12
您不能在任何地方实例化soundPool
对象,这就是为什么要得到:
Attempt to invoke virtual method 'int android.media.SoundPool.load(java.lang.String, int)' on a null object reference
你应该使用这样的方法:
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
...prior装样。
更多关于SoundPool的信息来自安卓开发者网站。尽管经过更仔细的检查,现在使用SoundPool
的首选方法是使用生成器,因为某些方法现在被废弃了:SoundPool.Builder
https://stackoverflow.com/questions/27605223
复制相似问题