2012-12-14 119 views
0

你好我目前得到錯誤的一些我的Android市場上的應用程序:OnListItemClick NullPointerException異常

這是堆棧跟蹤:

java.lang.NullPointerException 
    at ****.****.MainActivity.onListItemClick(MainActivity.java:110) 
    at android.app.ListActivity$2.onItemClick(ListActivity.java:319) 
    at android.widget.AdapterView.performItemClick(AdapterView.java:284) 
    at android.widget.ListView.performItemClick(ListView.java:3513) 
    at android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 
    at android.os.Handler.handleCallback(Handler.java:587) 
    at android.os.Handler.dispatchMessage(Handler.java:92) 
    at android.os.Looper.loop(Looper.java:130) 
    at android.app.ActivityThread.main(ActivityThread.java:3683) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:507) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:870) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628) 
    at dalvik.system.NativeStart.main(Native Method) 

代碼部分地方出了問題:

@Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 
     mMediaPlayer.stop(); 
     Sound s = (Sound) l.getItemAtPosition(position); 
     mMediaPlayer = MediaPlayer.create(this, s.getSoundResourceId()); 
     mMediaPlayer.start(); 
    } 

大多數時候它在工作(播放聲音),但有時它會產生一個nullpointerexception,我不知道爲什麼可能有MediaPlayer或super.onListItemClick()?

在此先感謝

+1

什麼是110? – Pablo

+0

mMediaPlayer.start(); – David

回答

1

可能s爲空?你能重現/調試嗎?

能否提供您的列表適配器實現的getItemAtPosition(位置)的代碼。並可換行

if (s != null) { 
     mMediaPlayer = MediaPlayer.create(this, s.getSoundResourceId()); 
     mMediaPlayer.start(); 
    } 

編輯。嗯,我是在錯誤的路線。

返回

a MediaPlayer object, or null if creation failed 

,然後試圖找出原因創建失敗:因爲如果你閱讀文檔,你應該換啓動媒體播放器與空檢查。