0

I am writing android application to get latitude and longitude. I did this in a aysnctask, but it says that the provider is null. this is the code:

private class GetCurrentAddress extends AsyncTask<String, Void, String[]> {
    @Override
    protected String[] doInBackground(String... urls) {

         locationManager=(LocationManager) getSystemService(context.LOCATION_SERVICE);
         criteria=new Criteria();
        String provider=locationManager.getBestProvider(criteria, false);
        Location loc=locationManager.getLastKnownLocation(provider);
        String[] a = null;
        //double 
        lat=loc.getLatitude();
        //double 
        lng=loc.getLongitude();
         currentLatitude = Double.toString(lat);
         currentLongitude = Double.toString(lng);
         a[0]=currentLatitude;
         a[1]=currentLongitude;
        return a;

    }

    @Override
    protected void onPostExecute(String[] resultString) {
        dialog.dismiss();
    Toast.makeText(getApplicationContext(), "latt="+resultString[0], Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "langg="+resultString[1], Toast.LENGTH_LONG).show();
    }
}

here is the LogCat:

12-03 22:13:35.184: E/AndroidRuntime(10016): FATAL EXCEPTION: AsyncTask #1
12-03 22:13:35.184: E/AndroidRuntime(10016): java.lang.RuntimeException: An error occured while executing doInBackground()
12-03 22:13:35.184: E/AndroidRuntime(10016):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.lang.Thread.run(Thread.java:856)
12-03 22:13:35.184: E/AndroidRuntime(10016): Caused by: java.lang.IllegalArgumentException: provider==null
12-03 22:13:35.184: E/AndroidRuntime(10016):    at android.location.LocationManager.getLastKnownLocation(LocationManager.java:1159)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at com.example.weather_our.MainActivity$GetCurrentAddress.doInBackground(MainActivity.java:132)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at com.example.weather_our.MainActivity$GetCurrentAddress.doInBackground(MainActivity.java:1)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
12-03 22:13:35.184: E/AndroidRuntime(10016):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)

line 132 in MainActivity is : Location loc=locationManager.getLastKnownLocation(provider);

roa.tah
  • 547
  • 2
  • 11
  • 25
  • possible duplicate of [LocationManager's getBestProvider returning null](http://stackoverflow.com/questions/10687409/locationmanagers-getbestprovider-returning-null) – John Boker Dec 03 '13 at 20:25

1 Answers1

0

Check to be sure you have the proper permission requested in your manifest.

From the API documentation:

Unless noted, all Location API methods require the ACCESS_COARSE_LOCATION or ACCESS_FINE_LOCATION permissions. If your application only has the coarse permission then it will not have access to the GPS or passive location providers. Other providers will still return location results, but the update rate will be throttled and the exact location will be obfuscated to a coarse level of accuracy.

Dale Wilson
  • 9,166
  • 3
  • 34
  • 52