Android 安卓可以';t在未调用looper.prepare()的线程内创建处理程序

Android 安卓可以';t在未调用looper.prepare()的线程内创建处理程序,android,timer,handler,timertask,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(); // Crea

我开始开发一个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);
  }
}
我得到了错误
致命异常:Timer-1:无法在未调用looper.prepare()的线程内创建处理程序,我认为问题在于我在另一个线程内创建了一个处理程序。

那么,还有其他方法可以获得相同的结果吗?

您的错误基本上是说您试图在非UIThread中执行某些操作。要在UI线程中运行它,只需使用以下命令:

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

您是否正在尝试线程的
Run
方法
Looper.prepare()在执行getLocation()时是否触摸它的视图@莫辛:不,我不叫活套anywhere@Carnal在getLocation()中,我只获取gps位置,这里的代码相同[link]谢谢!这也解决了我类似的问题。我试图从计时器的线程中获取GPS定位。嗯,我想没有UI线程吧?它应该如何做呢?