无法处理Handler

时间:2014-04-22 10:32:53

标签: android seekbar

我正在用我的SD卡播放音乐,我希望我的搜索栏在音乐播放时每2秒进行一次。我正在尝试使用Handler。但我不知道如何使用Handler coretly 这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    textView1 = (TextView) findViewById(R.id.textView1);



    button1.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            MediaPlayer mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/MyImages/.audio2.wav");
                mediaPlayer.prepare();
                mediaPlayer.start();

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       

             setupHandler();

        }
    });
}
private void setupHandler()
{

 handler.removeCallbacks(moveSeekBarThread);
  handler.postDelayed(moveSeekBarThread, 2000); //cal the thread after  millisec
}
private Runnable moveSeekBarThread = new Runnable() {

    public void run() {

        if(mediaPlayer.isPlaying()){

        int mediaPos = mediaPlayer.getCurrentPosition();
        int mediaMax = mediaPlayer.getDuration();
        seekBar1.setMax(mediaMax);
        seekBar1.setProgress(mediaPos);

        handler.postDelayed(this, 2000); //Looping the thread after  second
                                        // seconds
        }  
    }
};

}

我的应用程序在我按下按钮1后崩溃了2秒。所以我相信这里的问题是Handler。我是否将此处理程序称为错误或什么?

2 个答案:

答案 0 :(得分:2)

当您询问应用程序崩溃时,请始终发布您的logcat。 因此,在这种情况下,你在这里有NullPointerException,我认为: 如果(mediaPlayer.isPlaying()) 您没有初始化您的媒体播放器变量。

编辑:

MediaPlayer mediaPlayer;
    @Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    seekBar1 = (SeekBar) findViewById(R.id.seekBar1);
    button1 = (Button) findViewById(R.id.button1);
    button2 = (Button) findViewById(R.id.button2);
    textView1 = (TextView) findViewById(R.id.textView1);



    button1.setOnClickListener(new OnClickListener() 
    {

        @Override
        public void onClick(View v) 
        {
            mediaPlayer = new MediaPlayer();
            try {
                mediaPlayer.setDataSource(Environment.getExternalStorageDirectory()+"/MyImages/.audio2.wav");
                mediaPlayer.prepare();
                mediaPlayer.start();

            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (SecurityException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalStateException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }       

             setupHandler();

        }
    });
}
private void setupHandler()
{

 handler.removeCallbacks(moveSeekBarThread);
  handler.postDelayed(moveSeekBarThread, 2000); //cal the thread after  millisec
}
private Runnable moveSeekBarThread = new Runnable() {

    public void run() {

        if(mediaPlayer.isPlaying()){

        int mediaPos = mediaPlayer.getCurrentPosition();
        int mediaMax = mediaPlayer.getDuration();
        seekBar1.setMax(mediaMax);
        seekBar1.setProgress(mediaPos);

        handler.postDelayed(this, 2000); //Looping the thread after  second
                                        // seconds
        }  
    }
};

答案 1 :(得分:0)

使用以下代码在2秒后更新SeekBar。

在onCreate()中为Handler创建Object,如下所示。

Handler miHandler = new Handler();

添加将代码粘贴到线程中。

miHandler.postDelayed(new Runnable() {
       @Override
   public void run() {
       try{
                 seekBar1.setProgress(mediaPos+ add 2sec delay time also);
          }catch(Exception e) {e.printStrackTrace();}
   }
}, 1500);