2011-11-10 124 views
1

我爲了使音板像應用應用程序崩潰,當按鈕被點擊

public class ButtonSoundActivity extends Activity implements OnClickListener { 
    private Button buttons[]; 
    private MediaPlayer mediaPlayers[]; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main);  
    } 

    @Override 
    protected void onPause() { 
     super.onPause(); 
     releaseSounds(); 
    } 

    @Override 
    protected void onResume() { 
     super.onResume(); 
     initContent(); 
    } 

    @Override 
    protected void onStop() { 
     super.onStop(); 
     releaseSounds(); 
    } 

    /** 
    * Initialise all the content : 
    * buttons, media players and set click listeners for the buttons 
    */ 
    private void initContent() { 
     buttons = new Button[5]; 
     mediaPlayers = new MediaPlayer[5]; 

     buttons[0] = (Button)findViewById(R.id.button0); 
     buttons[1] = (Button)findViewById(R.id.button1); 
     buttons[2] = (Button)findViewById(R.id.button2); 
     buttons[3] = (Button)findViewById(R.id.button3); 
     buttons[4] = (Button)findViewById(R.id.button4); 

     mediaPlayers[0] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound1); 
     mediaPlayers[1] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound2); 
     mediaPlayers[2] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound3); 
     mediaPlayers[3] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound4); 
     mediaPlayers[4] = MediaPlayer.create(ButtonSoundActivity.this, R.raw.sound5); 


     for(int i=0; i<5; i++) { 
      buttons[i].setOnClickListener(this); 
     }  
    } 

    public void onClick(View v) { 
     stopSounds(); 
     for(int i=0; i<5; i++) 
      if(v.getId() == buttons[i].getId()) { 
       if(mediaPlayers[i] != null) 
       mediaPlayers[i].start(); 
      } 
    } 

    /** 
    * Stop the previous sound when another button is clicked 
    * so that the sounds don't overlap 
    */ 
    private void stopSounds() { 
     for(int i=0; i<5 ;i++) 
      if(mediaPlayers[i] != null) 
      if(mediaPlayers[i].isPlaying()) { 
       mediaPlayers[i].pause(); 
       mediaPlayers[i].seekTo(0); 
      } 
    } 

    /** 
    * Release all sounds and empty the mediaPlayers array 
    */ 
    private void releaseSounds() { 
     for(int i=0; i<5;i++) { 
      if(mediaPlayers[i] != null) { 
       mediaPlayers[i].stop(); 
       mediaPlayers[i].release(); 
       mediaPlayers[i] = null;     
      } 
     } 
    }  
} 

XML文件寫了這個代碼

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="button0" android:id="@+id/button0"/> 

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="button1" android:id="@+id/button1"/> 

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="button2" android:id="@+id/button2"/>  

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="button3" android:id="@+id/button3"/> 

    <Button android:layout_width="wrap_content" android:layout_height="wrap_content" 
     android:text="button4" android:id="@+id/button4"/>  
</LinearLayout> 

的問題是,如果我點擊一個按鈕多聲音不再播放,或者某個時候,當我第一次打開應用程序時,它會在我點擊一個按鈕時崩潰。 錯誤說,這是關係到stopSounds方法

error code

因此,沒有人有答案嗎?

編輯:我加入了,如果()之前每個MediaPlayer的元素來檢查自己是否爲空,還我跟着這個方法:

  AssetFileDescriptor fileDescriptor = getResources().openRawResourceFd(R.raw.sound1); 
     mediaPlayers[0] = new MediaPlayer(); 

     try { 
      mediaPlayers[0].setDataSource(fileDescriptor.getFileDescriptor()); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try { 
      mediaPlayers[0].prepare(); 
     } catch (IllegalStateException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

,但沒有任何運氣:(

編輯2:我得到了它的工作:),我必須確保該媒體播放器陣列是空的,當我重啓應用。我還添加了的onPause()的onResume()功能。

+0

你的代碼的第57行有一個NullPointerException,這行是什麼? – Egor

回答

3

你在第57行得到一個NullPointerException,你stopSounds方法內。你可以自己發表這一行嗎?在mediaPlayers對象

最有可能的一個爲空。嘗試單步執行調試器,並在首次輸入stopSounds方法時查看陣列的外觀。

+2

'如果(mediaPlayers [I] .isPlaying()){' 這是我甚至檢查線 –

+2

如果是空'\t \t \t \t如果(!mediaPlayers [I] .equals(空)) 如果(mediaPlayers [ i] .isPlaying()){ \t \t mediaPlayers [i] .pause(); \t \t mediaPlayers [i] .seekTo(0); }' –

+1

作爲文檔狀態,['MediaPlayer.create()'可以返回null如果創建失敗](http://developer.android.com/reference/android/media/MediaPlayer.html#create%28android.content .Context,%20int%29)。在嘗試調用數組中的MediaPlayer對象的方法之前嘗試進行空檢查 – Craigy

相關問題