简体   繁体   English

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

[英]Android: Handlers within Fragments *.post()?

I am migrating code from a monolithic activity to a fragment / pager setup prescribed by the Big Nerd Ranch (CriminalIntent ch 21, https://www.bignerdranch.com/solutions/AndroidProgramming2e.zip ). 我正在将代码从整体活动迁移到Big Nerd Ranch(CriminalIntent ch 21, https://www.bignerdranch.com/solutions/AndroidProgramming2e.zip )规定的片段/寻呼机设置。 Great book BTW :) 很棒的书顺便说一句:)

In doing so I am adding the following code below to the fragment CrimeCameraFragment to replace the camera photo recording with an audio recording. 为此,我将下面的代码添加到片段CrimeCameraFragment中,以用音频记录代替摄像机的照片记录。 When doing so, the Handler I'm trying to use doesn't have access to .post for some reason "error: cannot find symbol method post"? 这样做时,由于某种原因“我正在尝试使用的处理程序无法访问.post:错误:找不到符号方法post”?

public class Whatever extends fragment{

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

then it has the following timerTask: 然后它具有以下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();
        };
    }
};

The handler is null to start 处理程序为null即可启动

private Handler mHandler = null;

then has auto generated code later: 然后稍后会自动生成代码:

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

            @Override
            public void flush() {}

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

Any idea where my Handler.post functionality went? 知道我的Handler.post功能去了哪里吗?

Thanks! 谢谢!

Your Handler class is not the Android Handler class, and it's a Handler class with the same name in a java package. 您的Handler类不是Android Handler类,而是在Java包中具有相同名称的Handler类。 Just delete the import code and import again. 只需删除导入代码,然后再次导入即可。 Check that it must be from an android package. 检查它是否必须来自android程序包。 It should be android.os.Handler if I remembers correctly. 如果我没记错的话,应该是android.os.Handler

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM