Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
914 views
in Technique[技术] by (71.8m points)

android - requestLocationUpdates gives error "Can't create Handler inside thread that has not called Looper.prepare()

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);
    }
};
}

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

No need to even write a complicated solution:

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

This will work just fine.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...