我正在处理应用程序以查找用户位置。 在我的mapview中,我完成了完美的任务。 但找到位置我没有得到它。 下面是我的代码和logcat错误..
代码:
tv = (TextView)findViewById(R.id.tv1);
mlocationManager =(LocationManager)getSystemService(LOCATION_SERVICE) ;
Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
String locatinProvider = mlocationManager.getBestProvider(criteria,true);
Location mlocation = mlocationManager.getLastKnownLocation(locatinProvider);
tv.setText("Last location lat:" +mlocation.getLatitude() + "long:" + mlocation.getLongitude() );
logcat的:
02-17 17:13:12.260: E/AndroidRuntime(1215): FATAL EXCEPTION: main
02-17 17:13:12.260: E/AndroidRuntime(1215): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.satyam.curlocation/com.satyam.curlocation.CurLocationActivity}: java.lang.NullPointerException
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1622)
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1638)
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:928)
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.os.Handler.dispatchMessage(Handler.java:99)
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.os.Looper.loop(Looper.java:123)
02-17 17:13:12.260: E/AndroidRuntime(1215): at android.app.ActivityThread.main(ActivityThread.java:3647)
02-17 17:13:12.260: E/AndroidRuntime(1215): at java.lang.reflect.Method.invokeNative(Native Method)
02-17 17:13:12.260: E/AndroidRuntime(1215): at java.lang.reflect.Method.invoke(Method.java:507)
02-17 17:13:12.260: E/AndroidRuntime(1215): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-17 17:13:12.260: E/AndroidRuntime(1215): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-17 17:13:12.260: E/AndroidRuntime(1215): at dalvik.system.NativeStart.main(Native Method)
02-17 17:13:12.260: E/AndroidRuntime(1215): Caused by: java.lang.NullPointerException
答案 0 :(得分:4)
getLastKnownLocation()
返回null。请参阅the manual。
Location mlocation = mlocationManager.getLastKnownLocation(locatinProvider);
if(mlocation != null){
tv.setText("Last location lat:" +mlocation.getLatitude() + "long:" + mlocation.getLongitude() );
}else{
tv.setText("No last location available");
}
答案 1 :(得分:1)
在 Android ManifestFile.xml
中的活动下注册 MapActivity 类