Android 无法在未调用 looper.prepare() 的线程内创建处理程序

标签 android timer handler timertask

我开始开发一个录制视频的 Android 应用程序,我需要每 1 分钟收集一次 GPS 位置

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // Create an instance of Camera
    mCamera = getCameraInstance();

    // Create our Preview view and set it as the content of our activity.
    mPreview = new CameraPreview(this, mCamera);
    FrameLayout preview = (FrameLayout) findViewById(id.camera_preview);
    preview.addView(mPreview);

    private boolean isRecording = false;

    // Add a listener to the Capture button
    Button captureButton = (Button) findViewById(id.button_capture);
    captureButton.setOnClickListener(
      new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        if (isRecording) {
            // stop recording and release camera
            mMediaRecorder.stop();  // stop the recording
            releaseMediaRecorder(); // release the MediaRecorder object
            mCamera.lock();         // take camera access back from MediaRecorder

            // inform the user that recording has stopped
            setCaptureButtonText("Capture");
            isRecording = false;
        } else {
            // initialize video camera
            if (prepareVideoRecorder()) {
                // Camera is available and unlocked, MediaRecorder is prepared,
                // now you can start recording
                mMediaRecorder.start();

                // get the gps data
                GpsDataFile.getOutPutDataFile(DATA_TYPE_GPS);
                mTimer = new Timer;
                mTimer.schedule(new setGpsDataToFile(),0,1000)
                // inform the user that recording has started
                setCaptureButtonText("Stop");
                isRecording = true;
            } else {
                // prepare didn't work, release the camera
                releaseMediaRecorder();
                // inform user
            }
        }
    }
 }



Private Class setGpsDataToFile extends TimerTask {
  @Override
  public void run {
   // getLocation contains another timer task
   myLocation.getLocation(getApplicationContext(), mLocationResult); // I got the error here
   // write the gps data in a file
   gpsDataCapture(GpsDataFile, mLatitude, mLongitude);
  }
}

我收到错误 FATAL EXCEPTION: Timer-1: can't create handler inside thread that has not called looper.prepare() 我认为问题在于我在其他。 那么有没有其他方法可以得到相同的结果呢?

最佳答案

你的错误基本上是说你试图在非 UIThread 中做某事。要在 UI 线程中运行它,只需使用这个:

runOnUiThread(new Runnable() {
    public void run() {
        // do your work right here
    }
});

关于Android 无法在未调用 looper.prepare() 的线程内创建处理程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11359930/

相关文章:

java - 创建基于 GUI 的计时器(或秒表)

android - 在 Handler 中执行所有预定的(postDelayed)runnables

java - Android - 将嵌套 map 转换为 JSON 字符串

android - 如何在 Android 中使用 Javapoet 生成 StringDef

android - ScrollView 中的 TextView

java - NettyIO 断开客户端与服务器的连接

c - 对 SMC 调用的 Svc 处理程序

android - seeking时如何隐藏SeekBar的tickmark?

javascript - 当计时器结束时replaceWith

javascript - 在定时器内进行 AJAX 调用并保持值