Android soundpool 空指针异常
Posted
技术标签:
【中文标题】Android soundpool 空指针异常【英文标题】:Android soundpool null pointer exception 【发布时间】:2012-09-07 18:49:47 【问题描述】:我想用 soundpool 播放声音文件。我在播放音频文件时收到错误消息。 (“不幸的是,galeri 的进程已停止”)如何在滑动页面时播放短声音?谢谢。
代码是;
package com.example.galeri;
import com.example.galeri.R;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
public class MyPagerAdapter extends PagerAdapter
private static final Context Context = null;
SoundPool snd;
int sescal;
public int getCount()
return 6;
public Object instantiateItem(View collection, int position)
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
snd = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
sescal = snd.load(Context, R.raw.ayna, 1);
View view = null;
int resId = 0;
switch (position)
case 0:
resId = R.layout.farleft;
break;
case 1:
resId = R.layout.left; // second
view = inflater.inflate(resId, null);
snd.play(sescal, 0.9f, 0.9f, 1, -1, 1f);
break;
case 2:
resId = R.layout.middle;
break;
case 3:
resId = R.layout.right;
break;
case 4:
resId = R.layout.farright;
break;
case 5:
resId = R.layout.other;
break;
((ViewPager) collection).addView(view, 0);
return view;
private void setVolumeControlStream(int streamMusic)
// TODO Auto-generated method stub
private void setVolumeControlStream(int streamMusic)
// TODO Auto-generated method stub
@SuppressWarnings("unused")
private Context getBaseContext()
// TODO Auto-generated method stub
return null;
@SuppressWarnings("unused")
private PagerAdapter findViewById(int myfivepanelpager)
// TODO Auto-generated method stub
return null;
@Override
public void destroyItem(View arg0, int arg1, Object arg2)
((ViewPager) arg0).removeView((View) arg2);
@Override
public boolean isViewFromObject(View arg0, Object arg1)
return arg0 == ((View) arg1);
@Override
public Parcelable saveState()
return null;
public static Integer getItem(int position)
// TODO Auto-generated method stub
return null;
Logcat;
09-07 18:37:28.169: E/AndroidRuntime(653): FATAL EXCEPTION: main
09-07 18:37:28.169: E/AndroidRuntime(653): java.lang.NullPointerException
09-07 18:37:28.169: E/AndroidRuntime(653): at android.media.SoundPool.load(SoundPool.java:191)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.example.galeri.MyPagerAdapter.instantiateItem(MyPagerAdapter.java:30)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.PagerAdapter.instantiateItem(PagerAdapter.java:110)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:692)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.populate(ViewPager.java:821)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.populate(ViewPager.java:772)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1234)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.View.measure(View.java:15172)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer.doFrame(Choreographer.java:525)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.os.Handler.handleCallback(Handler.java:615)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.os.Handler.dispatchMessage(Handler.java:92)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.os.Looper.loop(Looper.java:137)
09-07 18:37:28.169: E/AndroidRuntime(653): at android.app.ActivityThread.main(ActivityThread.java:4745)
09-07 18:37:28.169: E/AndroidRuntime(653): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 18:37:28.169: E/AndroidRuntime(653): at java.lang.reflect.Method.invoke(Method.java:511)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-07 18:37:28.169: E/AndroidRuntime(653): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-07 18:37:28.169: E/AndroidRuntime(653): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
最好将 Context 成员变量命名为 mContext 而不是 Context 【参考方案1】:问题是您的 Context 变量被声明为 null 并且从未设置。
然后,在其 load()
方法中,SoundPool 调用 context.getResources().openRawResourceFd(resId)
并在此处获得 NullPointerException。
您可以从instantiateItem
方法的View collection
参数中获取有效的上下文。
所以尝试调用:
snd.load(collection.getContext(), R.raw.ayna, 1);
【讨论】:
sescal = snd.load(context.getResources().openRawResourceFd(resId), R.raw.ayna);真的吗??以上是关于Android soundpool 空指针异常的主要内容,如果未能解决你的问题,请参考以下文章
如何解决空指针异常和 ThreadException 中的错误android