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

Android: Handlers within Fragments *.post()?

我正在将代码从整体式 activity 迁移到 Big Nerd Ranch(CriminalIntent ch 21,https://www.bignerdranch.com/solutions/AndroidProgramming2e.zip)规定的片段/寻呼机设置。顺便说一句,好书 :)

这样做时,我将下面的代码添加到片段 CrimeCameraFragment 中,以用录音替换相机照片记录。这样做时,我尝试使用的处理程序无法访问 .post 出于某种原因 "error: cannot find symbol method 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();
        };
    }
};

处理程序为空,无法启动

private Handler mHandler = null;

稍后自动生成代码:

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

            @Override
            public void flush() {}

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

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

谢谢!

您的 Handler class 不是 Android Handler class,而是 Handler class java 包中的同名。只需删除导入代码并重新导入即可。检查它是否必须来自 android 包。没记错的话应该是android.os.Handler