位置提供者=null,安卓
Posted
技术标签:
【中文标题】位置提供者=null,安卓【英文标题】:Location provider=null, android 【发布时间】:2013-12-20 01:54:02 【问题描述】:我正在编写 android 应用程序来获取纬度和经度。我在 aysnctask 中执行此操作,但它说提供程序为空。这是代码:
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);
【问题讨论】:
LocationManager's getBestProvider returning null 的可能重复项 【参考方案1】:检查以确保您拥有清单中请求的适当权限。
来自 API 文档:
除非另有说明,否则所有 Location API 方法都需要 ACCESS_COARSE_LOCATION 或 ACCESS_FINE_LOCATION 权限。如果你的 应用程序只有粗略的权限,然后它就没有 访问 GPS 或被动位置提供程序。其他供应商将 仍然返回位置结果,但更新率将受到限制 并且确切的位置将被混淆到一个粗略的水平 准确性。
【讨论】:
以上是关于位置提供者=null,安卓的主要内容,如果未能解决你的问题,请参考以下文章
融合位置提供者:getLastKnowLocation 返回 null 并且 onLocationChange 没有被调用?