0

我做了一個應用程序,您可以通過圖像輕掃,並且您觸摸的每個圖像都會播放聲音。那麼,然後我有一個NullPointerException的問題。最好的部分是,首先點擊它實際上播放聲音。也許你會知道發生了什麼事?MediaPlayer中的ViewPagerAdapter中的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) 
+0

發生錯誤的cpde部分未發佈,請這樣做。 – Aashir

+0

@Aashir更新 –

+0

更新LogCat,因爲它現在指出真正的問題。 –

回答

1

根據文檔:

當用MediaPlayer的做,你應該呼叫release(),免費資源。如果未發佈,則過多的MediaPlayer實例將導致異常。

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

但在你的代碼永遠不會釋放你的媒體播放器。在instantiateItem

:在你的destroyItem方法,則需要釋放物體mediaPlayer,但它從未被初始化...

因此,也許你可以試試這個

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對象。

+0

我沒有得到邏輯o標籤getters和setters,但人 - 它的作品像一個魅力!即使我比用戶以前更快地完成動作,我現在也無法崩潰。非常感謝! –