0

我不确定为什么我在处理程序上收到错误。这些代码一直有效,直到我添加处理程序来控制搜索栏,它会提示错误。

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,谁能指出我的错误

4

1 回答 1

1

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

于 2013-05-09T07:37:08.203 回答