Search code examples
javaandroidandroid-fragmentsandroid-handlerandroid-audiorecord

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 ). 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. 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"?

public class Whatever extends fragment{

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

then it has the following 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

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?

Thanks!


Solution

  • Your Handler class is not the Android Handler class, and it's a Handler class with the same name in a java package. Just delete the import code and import again. Check that it must be from an android package. It should be android.os.Handler if I remembers correctly.