requestLocationUpdates给出错误"内螺纹已经不叫尺蠖prepare无法创建处理程序() [英] requestLocationUpdates gives error "Can't create Handler inside thread that has not called Looper.prepare()

查看:127
本文介绍了requestLocationUpdates给出错误"内螺纹已经不叫尺蠖prepare无法创建处理程序()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这样的问题存在,但我很困惑在这里。我使用这个code:

I know this sort of question exists but I'm confused here. I'm using this code:

    public class NewWaitAppActivity extends Activity {
    private Handler mHandler;
    @Override
        public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        mHandler = new Handler();
        lcmgr = (LocationManager)this.getSystemService(Context.LOCATION_SERVICE);
        Thread LocThread = new Thread(mLocationUpdater);
        Log.d("TAG","About to start worker thread");
        LocThread.start();
}

public void startTimer(View v) {
        if (mStartTime == 0L) {
            mStartTime = System.currentTimeMillis();
            mHandler.removeCallbacks(mUpdateTimeTask);
            mHandler.postDelayed(mUpdateTimeTask, 100);
        }
    }

private Runnable mUpdateTimeTask = new Runnable() {
        public void run() {
            final long start = mStartTime;
            long millis = System.currentTimeMillis() - start;
            int seconds = (int) (millis / 1000);
            int minutes = seconds / 60;
            seconds = seconds % 60;

            TextView timer = (TextView) findViewById(R.id.time_elapsed);

            if (seconds < 10) {
                timer.setText("" + minutes + ":0" + seconds);
            } else {
                timer.setText("" + minutes + ":" + seconds);
            }

            mHandler.postDelayed(this, 1000);
        }
    };
LocationListener locationListener = new LocationListener() {

        public void onLocationChanged(Location location) {
          // Called when a new location is found by the network location provider.
          mStopTimer();
        }
};


private Runnable mLocationUpdater = new Runnable(){
        public void run(){

            Log.d("TAG","Inside worker thread");
            lcmgr.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
    }
};
}

我基本上试图阻止主线程时,位置更新的定时器。该应用程序甚至不启动,它使运行时上面提到的错误。的错误是由于requestLocationUpdates()。好像我不能叫stopTimer()内onLocationChanged()。我怎样才能解决这个?

I am basically trying to stop the timer of the main thread when a location updates. The application doesn't even start, it gives the error mentioned above during runtime. The error is due to requestLocationUpdates(). It seems like I can't call stopTimer() inside onLocationChanged(). How can I correct this?

推荐答案

从最新创建的线程调用lcmgr.requestLocationUpdates(......),当它本身是一个异步回调化机制的需要?< BR>刚刚移动这个code在OnCreate(),您的活动和事情应该很好地工作。
当您试图创建一个线程,不打算在环内的处理程序
另外这个错误出现所有的(即是不线程调用活套。prepare,为您的堆栈跟踪州也)有限公司关于此错误的解释,可以在这个网站上找到也。
但是一个很好的解释这里也给出
的http:// www.aviyehuda.com/2010/12/android-multithreading-in-a-ui-environment/

whats the need of calling "lcmgr.requestLocationUpdates(....)" from the thread you created, when it is itself an asynchronous callback based mechanism?
Just move this code in the onCreate() of your activity and things should work fine.

Also this error comes when you try to create a handler inside a thread which is not intended to loop at all (ie. the threads that dont call Looper.prepare, as your stack trace states also).

More explanation about this error can be found in this site also.
But a good explanation is also given here.
http://www.aviyehuda.com/2010/12/android-multithreading-in-a-ui-environment/

这篇关于requestLocationUpdates给出错误&QUOT;内螺纹已经不叫尺蠖prepare无法创建处理程序()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆