5

ビデオを録画する 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()、問題は、ハンドラーを他のハンドラー内に作成することだと思います。同じ結果を得る別の方法はありますか?

4

1 に答える 1

19

あなたのエラーは基本的に、非 UIThread で何かをしようとしていると言っています。UI スレッドで実行するには、次のようにします。

runOnUiThread(new Runnable() {
    public void run() {
        // do your work right here
    }
});
于 2012-07-06T10:03:11.993 に答える