2013-07-14 83 views
0

這是我的代碼如下。 我在我的應用程序中有一個meidaplayer。如果用戶播放音樂,則所有事情都是正確的,但當用戶離開活動而不播放音樂時,會導致力量關閉。android onstop方法導致強制關閉

有什麼問題?

public class Elementary1Activity extends Activity implements Runnable, OnClickListener, OnSeekBarChangeListener{ 
     private SeekBar seekBar; 
     private ImageButton startMedia; 
     private ImageButton pauseMedia; 
     private MediaPlayer mp; 

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

      TextView title1 = (TextView) findViewById(R.id.title1); 
      TextView tx1 = (TextView) findViewById(R.id.textView1); 
      Typeface font = Typeface.createFromAsset(getAssets(), "font.ttf"); 
      title1.setTypeface(font); 
      tx1.setTypeface(font); 

      AudioControl();  



     } 



     public void AudioControl(){ 
      seekBar = (SeekBar) findViewById(R.id.seekBarnew2); 
      startMedia = (ImageButton) findViewById(R.id.playbuttonnew2); 
      pauseMedia = (ImageButton) findViewById(R.id.pausebuttonnew2); 
      seekBar.setOnSeekBarChangeListener(this); 
      startMedia.setOnClickListener(this); 
      pauseMedia.setOnClickListener(this); 
     } 



     public void run() { 
      int currentPosition= 0; 
      int total = mp.getDuration(); 
      while (mp!=null && currentPosition<total) { 
       try { 
        Thread.sleep(1000); 
        currentPosition= mp.getCurrentPosition(); 
       } catch (InterruptedException e) { 
        return; 
       } catch (Exception e) { 
        return; 
       }    
       seekBar.setProgress(currentPosition); 
      } 
     } 

     public void onClick(View v) { 
      pauseMedia.setVisibility(View.VISIBLE); 
      seekBar.setVisibility(View.VISIBLE); 
      startMedia.setVisibility(View.INVISIBLE); 
      if (v.equals(startMedia)) { 
       if (mp != null && mp.isPlaying()) return; 
       if(seekBar.getProgress() > 0) { 
        mp.start(); 
        return; 
       } 
       mp = MediaPlayer.create(Elementary1Activity.this, R.raw.e1); 
       mp.start();    

       seekBar.setProgress(0); 
       seekBar.setMax(mp.getDuration()); 
       new Thread(this).start(); 
      } 

      if (v.equals(pauseMedia) && mp!=null) { 
       mp.pause(); 
       pauseMedia.setVisibility(View.INVISIBLE); 
       startMedia.setVisibility(View.VISIBLE); 

      }  

     } 



     public void onStartTrackingTouch(SeekBar seekBar) { 
     } 

     public void onStopTrackingTouch(SeekBar seekBar) { 
     } 

     public void onProgressChanged(SeekBar seekBar, int progress, 
       boolean fromUser) { 
      if(fromUser) mp.seekTo(progress); 

     } 

     public void onStop() { 
       super.onStop(); 
       mp.stop(); 
      } 
    } 
+1

什麼是關閉力量的例外?我認爲你應該檢查mp是否爲空,並用try/catch塊包圍你的代碼行 – Aerilys

回答

0

至於我能讀懂你的代碼賴特,如果用戶離開你的活動,這意味着用戶使用後退按鈕或做其他事,使應用程序停止,對不對?如果是的話,我認爲問題在於,您目前不會初始化您的MediaPlayer。您首先在Button Click上初始化此MediaPlayer,但是在onStop()中停止未初始化的MediaPlayer。初始化中的onCreate(MediaPlayer的):

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

     TextView title1 = (TextView) findViewById(R.id.title1); 
     TextView tx1 = (TextView) findViewById(R.id.textView1); 
     Typeface font = Typeface.createFromAsset(getAssets(), "font.ttf"); 
     title1.setTypeface(font); 
     tx1.setTypeface(font); 
     mp = MediaPlayer.create(Elementary1Activity.this, R.raw.e1); 


     AudioControl();  



    } 

我haven't測試你的代碼,但如果這doesn't工作還存在另一個錯誤,這是我hadn't搜索。但這絕對是錯誤的。

相關問題