位置提供者= null,android

时间:2013-12-03 20:15:32

标签: android

我正在编写android应用程序来获取经度和纬度。我在一个aysnctask中做了这个,但它说提供者是null。这是代码:

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

这是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)
MainActivity中的第132行是: 位置loc = locationManager.getLastKnownLocation(provider);

1 个答案:

答案 0 :(得分:0)

检查以确保您的清单中有正确的权限。

来自API文档:

  

除非另有说明,否则所有Location API方法都需要   ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限。如果你的   应用程序只有粗略的权限,然后它不会有   访问GPS或被动位置提供商。其他提供商会   仍然返回位置结果,但更新率将受到限制   并且确切的位置将被混淆到粗略的水平   精度。