ViewPagerAdapter中MediaPlayer中的NullPointerException

时间:2014-01-10 18:01:17

标签: android eclipse nullpointerexception android-viewpager android-mediaplayer

我做了一个应用程序,你可以刷过图像和你触摸的每个图像播放声音。好吧,然后我对NullPointerException有一点问题。最好的部分是,首先点击它实际播放声音。也许你会知道发生了什么?

代码如下所示:

public class ViewPagerAdapter extends PagerAdapter {
// Declare Variables
Context context;
String[] rank;;
int[] gun;
int[] sounds;
LayoutInflater inflater;
MediaPlayer mediaPlayer;

public ViewPagerAdapter(Context context, String[] rank, int[] gun, int[] sounds) {
    this.context = context;
    this.rank = rank;
    this.sounds = sounds;
    this.gun = gun;
}

@Override
public int getCount() {
    return rank.length;
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

@Override
public Object instantiateItem(ViewGroup container, final int position) {

    // Declare Variables
    final ImageView imggun;


    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);

    // Locate the ImageView in viewpager_item.xml
    imggun = (ImageView) itemView.findViewById(R.id.gun);
    // Capture position and set to the ImageView
    imggun.setImageResource(gun[position]);

    // Add viewpager_item.xml to ViewPager
    ((ViewPager) container).addView(itemView);  

    int file = sounds[position];
    final MediaPlayer mp = MediaPlayer.create(context, file);

    imggun.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            mp.start();

        }
    });

    return itemView;
}

protected Context getApplicationContext() {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    ((ViewPager) container).removeView((RelativeLayout) object);
    mediaPlayer.release();
}
}

和LogCat:

01-10 21:12:02.964: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:03.534: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:03.634: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:03.654: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:05.004: W/MediaPlayer(12987): info/warning (1, 902)
01-10 21:12:05.004: I/MediaPlayer(12987): Info (1,902)
01-10 21:12:05.874: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:06.084: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:06.754: W/MediaPlayer(12987): info/warning (1, 902)
01-10 21:12:06.754: I/MediaPlayer(12987): Info (1,902)
01-10 21:12:08.434: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:08.594: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:08.944: W/MediaPlayer(12987): info/warning (1, 902)
01-10 21:12:08.944: I/MediaPlayer(12987): Info (1,902)
01-10 21:12:10.554: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:10.674: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:11.694: W/MediaPlayer(12987): info/warning (1, 902)
01-10 21:12:11.694: I/MediaPlayer(12987): Info (1,902)
01-10 21:12:12.604: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:12.764: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:12.874: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:12.874: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:14.134: W/MediaPlayer(12987): info/warning (1, 902)
01-10 21:12:14.134: I/MediaPlayer(12987): Info (1,902)
01-10 21:12:15.174: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:15.324: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:15.834: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:15.844: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:17.884: W/MediaPlayer(12987): info/warning (1, 26)
01-10 21:12:17.884: E/MediaPlayer(12987): error (1, -17)
01-10 21:12:17.914: D/MediaPlayer(12987): create failed:
01-10 21:12:17.914: D/MediaPlayer(12987): java.io.IOException: Prepare failed.:     status=0x1
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.media.MediaPlayer.prepare(Native Method)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.media.MediaPlayer.create(MediaPlayer.java:674)
01-10 21:12:17.914: D/MediaPlayer(12987):   at com.example.gunsounds.ViewPagerAdapter.instantiateItem(ViewPagerAdapter.java:66)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.support.v4.view.ViewPager.addNewItem(ViewPager.java:832)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.support.v4.view.ViewPager.populate(ViewPager.java:1048)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.support.v4.view.ViewPager.populate(ViewPager.java:914)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.support.v4.view.ViewPager$3.run(ViewPager.java:244)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.support.v4.view.ViewPager.completeScroll(ViewPager.java:1761)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.support.v4.view.ViewPager.onInterceptTouchEvent(ViewPager.java:1896)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:851)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:871)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:871)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:871)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:871)
01-10 21:12:17.914: D/MediaPlayer(12987):   at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1701)
01-10 21:12:17.914: D/MediaPlayer(12987):   at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1116)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.app.Activity.dispatchTouchEvent(Activity.java:2093)
01-10 21:12:17.914: D/MediaPlayer(12987):   at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1685)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.view.ViewRoot.handleMessage(ViewRoot.java:1802)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.os.Looper.loop(Looper.java:143)
01-10 21:12:17.914: D/MediaPlayer(12987):   at android.app.ActivityThread.main(ActivityThread.java:4914)
01-10 21:12:17.914: D/MediaPlayer(12987):   at java.lang.reflect.Method.invokeNative(Native Method)
01-10 21:12:17.914: D/MediaPlayer(12987):   at java.lang.reflect.Method.invoke(Method.java:521)
01-10 21:12:17.914: D/MediaPlayer(12987):   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-10 21:12:17.914: D/MediaPlayer(12987):   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-10 21:12:17.914: D/MediaPlayer(12987):   at dalvik.system.NativeStart.main(Native Method)
01-10 21:12:17.954: I/MediaPlayer(12987): Info (1,26)
01-10 21:12:18.024: W/MediaPlayer(12987): info/warning (1, 44)
01-10 21:12:18.024: I/MediaPlayer(12987): Info (1,44)
01-10 21:12:19.204: D/AndroidRuntime(12987): Shutting down VM
01-10 21:12:19.204: W/dalvikvm(12987): threadid=1: thread exiting with uncaught exception (group=0x40028a00)
01-10 21:12:19.224: E/AndroidRuntime(12987): FATAL EXCEPTION: main
01-10 21:12:19.224: E/AndroidRuntime(12987): java.lang.NullPointerException
01-10 21:12:19.224: E/AndroidRuntime(12987):    at com.example.gunsounds.ViewPagerAdapter$1.onClick(ViewPagerAdapter.java:73)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at android.view.View.performClick(View.java:2408)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at android.view.View$PerformClick.run(View.java:8817)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at android.os.Handler.handleCallback(Handler.java:587)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at android.os.Handler.dispatchMessage(Handler.java:92)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at android.os.Looper.loop(Looper.java:143)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at android.app.ActivityThread.main(ActivityThread.java:4914)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at java.lang.reflect.Method.invokeNative(Native Method)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at java.lang.reflect.Method.invoke(Method.java:521)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-10 21:12:19.224: E/AndroidRuntime(12987):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:1)

根据文件:

完成MediaPlayer后,您应该调用release()来释放资源。如果没有发布,太多的MediaPlayer实例将导致异常。

http://developer.android.com/reference/android/media/MediaPlayer.html#create(android.content.Context,int)

但在您的代码中,您永远不会发布您的媒体播放器。在您的destroyItem方法中,您正在释放一个对象mediaPlayer,但它从未初始化...

所以也许你可以试试这个:

instantiateItem

View itemView = inflater.inflate(R.layout.viewpager_item, container,
            false);
// what you need to do

itemView.setTag(mp);
return itemView;

在你destroyItem中:

@Override
public void destroyItem(ViewGroup container, int position, Object object) {
    // Remove viewpager_item.xml from ViewPager
    RelativeLayout layout = (RelativeLayout) object;
    MediaPlayer mp = (MediaPlayer)layout.getTag();
    mp.release();
    ((ViewPager) container).removeView(layout);

}

如果您仍然遇到崩溃,请查看MediaPlayer的创建,如果传递给Create()方法的int不是正确的资源,则可能会导致null对象。