Android:Fragments * .post()中的处理程序?

时间:2015-10-08 15:38:03

标签: java android android-fragments android-handler android-audiorecord

我正在将代码从单一活动迁移到Big Nerd Ranch规定的片段/寻呼机设置(CriminalIntent ch 21,https://www.bignerdranch.com/solutions/AndroidProgramming2e.zip)。好书BTW:)

在这样做的过程中,我将以下代码添加到片段CrimeCameraFragment中,以用录音取代相机照片录制。这样做时,我试图使用的处理程序由于某种原因无法访问.post“错误:无法找到符号方法帖子”?

public class Whatever extends fragment{

the onCreateView code{
button.setOnClickListener(...){
    audioRecord.startRecording();
    mHandler.post(updateTimerThread);
    };

然后它有以下timerTask:

//timer code ---------------------------------------------------------------------
public TimerTask updateTimerThread = new TimerTask(){
    public void run(){
        currentTime = SystemClock.uptimeMillis();
        elapsedTime = currentTime - startTime;
        if(elapsedTime< recordingDurationInMilliseconds)
        {
            //First we read the audio buffer -----------
            int bufferRead = audioRecord.read(buffer, 0, readBufferSize);
            double dataDbl = processData(buffer, bufferRead);

            //Put the data and time stamp into arrays
            dataArray.add(dataDbl);
            timeArray.add(elapsedTime);

            //then fire up a new runnable
            mHandler.postDelayed(this, delayMillis);
        }
        else //if elapsed time is > max record time
        {
            stopRecording();
        };
    }
};

处理程序为null以启动

private Handler mHandler = null;

然后会自动生成代码:

mHandler = new Handler() {
            @Override
            public void close() {}

            @Override
            public void flush() {}

            @Override
            public void publish(LogRecord record) {}
        };

知道我的Handler.post功能在哪里?

谢谢!

1 个答案:

答案 0 :(得分:2)

您的Handler类不是Android Handler类,它是java包中具有相同名称的Handler类。只需删除导入代码并再次导入即可。检查它必须来自android包。如果我记得正确,那应该是android.os.Handler