简体   繁体   English

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

[英]Android can't create handler inside thread that has not called looper.prepare()

I started developing an Android application that record a video and I need to gather the GPS location every 1 minute 我开始开发一个可录制视频的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);
  }
}

I got the error FATAL EXCEPTION: Timer-1: can't create handler inside thread that has not called looper.prepare() and I think the problem is that I create a handler inside an other. 我收到错误FATAL EXCEPTION: Timer-1: can't create handler inside thread that has not called looper.prepare()错误FATAL EXCEPTION: Timer-1: can't create handler inside thread that has not called looper.prepare() ,我认为问题是我在另一个FATAL EXCEPTION: Timer-1: can't create handler inside thread that has not called looper.prepare()内创建了处理程序。 So is there an other way to get the same result? 那么还有其他方法可以得到相同的结果吗?

Your error is basically saying that you're trying to do something in a non UIThread. 您的错误基本上是说您正在尝试在非UIThread中执行某些操作。 To run it in the UI Thread, just use this: 要在UI线程中运行它,只需使用以下命令:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android处理程序:无法在尚未调用Looper.prepare()的线程内创建处理程序 - Android handler: Can't create handler inside thread that has not called Looper.prepare() Android线程错误-无法在未调用Looper.prepare()的线程内创建处理程序 - Android thread error - Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序 - Can't create handler inside thread that has not called Looper.prepare() Android Android AsyncTask [无法在未调用Looper.prepare()的线程内创建处理程序 - Android AsyncTask [Can't create handler inside thread that has not called Looper.prepare()] java.lang.RuntimeException:无法在未在Android中调用Looper.prepare()的线程内创建处理程序 - java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() in Android Android致命异常:无法在未调用Looper.prepare()的线程内创建处理程序 - Android fatal exception: Can't create handler inside thread that has not called Looper.prepare() Android等待进度条无法在未调用Looper.prepare()错误的线程内创建处理程序 - Android Waiting progress bar Can't create handler inside thread that has not called Looper.prepare() error 警报对话框崩溃Android测试 - “无法在未调用Looper.prepare()的线程内创建处理程序” - Alert Dialog crashes Android test - “Can't create handler inside thread that has not called Looper.prepare()” Android上的Junit测试领域。 无法在未调用Looper.prepare()的线程内创建处理程序 - Junit Testing Realm on Android. Can't create handler inside thread that has not called Looper.prepare() 无法在未调用Looper.prepare()Android的线程内创建处理程序。 循环计时器 - Can't create handler inside thread that has not called Looper.prepare() Android. Loop in a timer
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM