2012-07-06 59 views
3

,我開始開發Android應用程序,錄製視頻,我需要收集的GPS定位每隔1分鐘的Android無法創建內螺紋處理程序尚未調用looper.prepare()

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(),我認爲問題是我在另一個內部創建了一個處理程序。 那麼還有其他方法可以得到相同的結果嗎?

+0

你嘗試在你線程的'Run'方法'Looper.prepare();' – 2012-07-06 09:56:47

+0

你觸摸它的觀點做時的getLocation(); – Carnal 2012-07-06 09:59:29

+0

@Mohsin不,我不打電話looper.prepare任何地方 – user1331120 2012-07-06 10:03:06

回答

16

你的錯誤基本上是說你試圖在非UIThread中做一些事情。在UI線程中運行,只需要使用這樣的:

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

謝謝!解決問題 – user1331120 2012-07-06 11:07:38

+1

很高興能幫到你:) – Carnal 2012-07-06 11:10:21

+0

謝謝!這也解決了我的類似問題。我試圖從一個定時器的線程內部得到一個GPS修復。 – stackoverflowuser2010 2012-07-12 06:30:30

相關問題