如何在Android中正确获取当前位置?使用Location和LocationManager类

时间:2014-12-10 17:41:13

标签: java android gps location

我正在开发一个从Network获取当前位置的应用程序,但是我在该行代码中收到错误:locationManager.requestLocationUpdates(provider,0,0,this);

public void getLocation(){
    locationManager = (LocationManager) context.getSystemService(Context.LOCATION_SERVICE);
    Criteria criteria = new Criteria();
    criteria.setAccuracy(Criteria.ACCURACY_FINE);
    String provider = locationManager.getBestProvider(criteria, true);

    this.location = locationManager.getLastKnownLocation(provider);

    if(location != null){
        this.latitude = location.getLatitude();
        this.longitude = location.getLongitude();
    }
    locationManager.requestLocationUpdates(provider, 0, 0, this);

}

我在AsyncTask的doInBackground方法中调用此类的对象。

这是我的logcat:

12-10 14:47:42.939: E/AndroidRuntime(12450): FATAL EXCEPTION: AsyncTask #1
12-10 14:47:42.939: E/AndroidRuntime(12450): Process: com.fb.newcomersapp, PID: 12450
12-10 14:47:42.939: E/AndroidRuntime(12450): java.lang.RuntimeException: An error occured while executing doInBackground()
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.os.AsyncTask$3.done(AsyncTask.java:300)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.util.concurrent.FutureTask.run(FutureTask.java:242)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.lang.Thread.run(Thread.java:841)
12-10 14:47:42.939: E/AndroidRuntime(12450): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.os.Handler.<init>(Handler.java:200)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.os.Handler.<init>(Handler.java:114)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.location.LocationManager$ListenerTransport$1.<init>(LocationManager.java:221)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.location.LocationManager$ListenerTransport.<init>(LocationManager.java:221)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.location.LocationManager.wrapListener(LocationManager.java:844)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:857)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.location.LocationManager.requestLocationUpdates(LocationManager.java:454)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at com.fb.newcomersapp.LocationTracker.getLocation(LocationTracker.java:48)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at com.fb.newcomersapp.FeedActivity$LocationAsyncTask.doInBackground(FeedActivity.java:105)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at com.fb.newcomersapp.FeedActivity$LocationAsyncTask.doInBackground(FeedActivity.java:1)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at android.os.AsyncTask$2.call(AsyncTask.java:288)
12-10 14:47:42.939: E/AndroidRuntime(12450):    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-10 14:47:42.939: E/AndroidRuntime(12450):    ... 4 more

0 个答案:

没有答案