当runnable和在android应用程序中添加handler.postdelayed()时,seekbar不工作

时间:2013-05-09 07:33:17

标签: android handler runnable

我不确定为什么我会在处理程序上出错。代码工作,直到我添加处理程序来控制搜索条,它将提示错误。

package com.voice.recording.v3;

import android.app.Activity;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;

public class VoiceRecordingPlaybackv3Activity extends Activity implements      OnCompletionListener, 
OnTouchListener, OnClickListener, OnSeekBarChangeListener {

MediaPlayer mediaPlayer;
View theView;
Button stopButton, startButton, quitButton;
SeekBar seekBar;
Handler handler;

int position = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    stopButton = (Button)this.findViewById(R.id.StopButton);
    startButton = (Button)this.findViewById(R.id.StartButton);
    quitButton = (Button)this.findViewById(R.id.quitButton);

    startButton.setOnClickListener(this);
    stopButton.setOnClickListener(this);
    quitButton.setOnClickListener(this);

    theView = this.findViewById(R.id.theview);

    theView.setOnTouchListener(this);

    mediaPlayer = MediaPlayer.create(this, R.raw.goodmorningandroid);
    mediaPlayer.setOnCompletionListener(this);

    seekBar = (SeekBar) findViewById(R.id.seekBar1);
    seekBar.setOnSeekBarChangeListener(this);
    seekBar.setMax(mediaPlayer.getDuration());

}

public void onCompletion(MediaPlayer mp){
    mediaPlayer.start();
    mediaPlayer.seekTo(position);
}

public boolean onTouch(View v, MotionEvent me){
    if(me.getAction() == MotionEvent.ACTION_MOVE){
        if(mediaPlayer.isPlaying()){
            position = (int)(me.getX()*
                    mediaPlayer.getDuration()/theView.getWidth());
            Log.v("SEEK",""+position);
            mediaPlayer.seekTo(position);
        }
    }

    return true;
}

public void onClick(View v){
    if(v == stopButton){
        mediaPlayer.pause();
    }else if(v == startButton){
        try{
            mediaPlayer.start();
            startPlayProgressUpdater();
        }catch(IllegalStateException e){
            mediaPlayer.pause();
        }
    }else if(v == quitButton ){
        mediaPlayer.stop();
        mediaPlayer.release();
    }
}

public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

}

public void onStartTrackingTouch(SeekBar seekBar) {

}


public void onStopTrackingTouch(SeekBar seekBar) {
    seekBar.setSecondaryProgress(seekBar.getProgress());
}

public void startPlayProgressUpdater() {
    seekBar.setProgress(mediaPlayer.getCurrentPosition());

    if (mediaPlayer.isPlaying()) {
        Runnable notification = new Runnable() {
            public void run() {
                startPlayProgressUpdater();
            }
        };
        handler.postDelayed(notification,100);
    }else{
        mediaPlayer.pause();
        seekBar.setProgress(0);
        }
    }
}

我得到的错误是NullPointerException,任何人都可以指出我的错误

1 个答案:

答案 0 :(得分:1)

您的代码显示处理程序是由未初始化定义的。如果不是问题,请发布stacktrace。