【问题标题】:requestLocationUpdates gives error "Can't create Handler inside thread that has not called Looper.prepare()requestLocationUpdates 给出错误“无法在未调用 Looper.prepare() 的线程内创建处理程序
【发布时间】:2018-10-03 04:08:57
【问题描述】:

我知道存在此类问题,但我在这里感到困惑。我正在使用此代码:

    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() 造成的。好像我不能在 onLocationChanged() 中调用 stopTimer()。我该如何纠正这个问题?

【问题讨论】:

    标签: android


    【解决方案1】:

    甚至不需要写一个复杂的解决方案:

    locationManager.requestLocationUpdates(provider, 5000, 0, mylistener, Looper.getMainLooper());
    

    这样就可以了。

    【讨论】:

    • 不幸的是,这并没有为我解决这个问题。 :(
    • 很好的解决方案!
    • Looper.getMainLooper() 已修复。我的代码在修复之前使用了Looper.mylooper()
    • 这应该是答案,尤其是当您在活动之外获得位置时。
    【解决方案2】:

    当它本身是一个基于异步回调的机制时,从您创建的线程调用“lcmgr.requestLocationUpdates(....)”有什么需要?
    只需将此代码移动到活动的 onCreate() 中

    当您尝试在根本不打算循环的线程内创建处理程序时,也会出现此错误(即不调用 Looper.prepare 的线程,作为您的堆栈跟踪状态)。

    关于这个错误的更多解释也可以在这个网站上找到。
    但是这里也给出了一个很好的解释。
    http://www.aviyehuda.com/2010/12/android-multithreading-in-a-ui-environment/

    【讨论】:

    • 虽然使用主线程将其放在 onCreate() 中确实可以工作...从线程调用lcmgr.requestLocationUpdates(....) 并非没有用,但这取决于应用程序在做什么.如果应用程序需要在没有 Activity 打开时从后台服务请求位置更新... onCreate() 解决方案将不起作用... RobbyLebotha 的解决方案是要走的路
    【解决方案3】:

    您无法访问/生成 Event 线程特定的事件,并且您不需要在线程中请求位置更新,而是可以使用服务来实现。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-08-15
      • 2011-09-06
      • 1970-01-01
      相关资源
      最近更新 更多