我正在将代码从单片活动迁移到Big Nerd Ranch (CriminalIntent ch 21, https://www.bignerdranch.com/solutions/AndroidProgramming2e.zip)规定的片段/寻呼机设置。不错的书,顺便说一句:)
在这样做时,我将以下代码添加到片段CrimeCameraFragment中,以将相机照片记录替换为音频记录。当这样做时,我试图使用的处理程序没有访问.post的某种原因"错误:找不到符号方法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
类不是Android的Handler
类,它是一个在java包中同名的Handler
类。只需删除导入代码并再次导入即可。检查它必须来自android包。如果我没记错的话应该是android.os.Handler