每當我嘗試獲取地圖時,地圖崩潰,logcat中就沒有任何東西。獲取當前位置的Google地圖
對不起基本要求,但我是Android的新手。
在此先感謝!
我的代碼:
private void setupMap() {
// TODO Auto-generated method stub
googlemap.setMyLocationEnabled(true);
//get the loacation manger object form system service
LocationManager locationManager=(LocationManager)getSystemService(LOCATION_SERVICE);
//get the criteria object for provider
Criteria criteria= new Criteria();
//get the name of best provider
String provider=locationManager.getBestProvider(criteria,true);
//get current Location
Location currentLocation=locationManager.getLastKnownLocation(provider);
//set maptype
googlemap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
//Get lattitude and longitude
//double latitude=currentLocation.getLatitude();
//double longitude=currentLocation.getLongitude();
//Toast.makeText(getBaseContext(),String.valueOf(latitude), Toast.LENGTH_SHORT).show(); //enable my location layer of google map
}
日誌貓:
12-04 12:16:11.652: E/AndroidRuntime(8811): FATAL EXCEPTION: main
12-04 12:16:11.652: E/AndroidRuntime(8811): Process: com.example.test, PID: 8811
12-04 12:16:11.652: E/AndroidRuntime(8811): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.map}: java.lang.NullPointerException
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2350)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.ActivityThread.access$800(ActivityThread.java:163)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1257)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.os.Looper.loop(Looper.java:157)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.ActivityThread.main(ActivityThread.java:5335)
12-04 12:16:11.652: E/AndroidRuntime(8811): at java.lang.reflect.Method.invokeNative(Native Method)
12-04 12:16:11.652: E/AndroidRuntime(8811): at java.lang.reflect.Method.invoke(Method.java:515)
12-04 12:16:11.652: E/AndroidRuntime(8811): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
12-04 12:16:11.652: E/AndroidRuntime(8811): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
12-04 12:16:11.652: E/AndroidRuntime(8811): at dalvik.system.NativeStart.main(Native Method)
12-04 12:16:11.652: E/AndroidRuntime(8811): Caused by: java.lang.NullPointerException
12-04 12:16:11.652: E/AndroidRuntime(8811): at com.example.test.map.setupMap(map.java:63)
12-04 12:16:11.652: E/AndroidRuntime(8811): at com.example.test.map.onCreate(map.java:36)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.Activity.performCreate(Activity.java:5389)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-04 12:16:11.652: E/AndroidRuntime(8811): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2256)
12-04 12:16:11.652: E/AndroidRuntime(8811): ... 11 more
我有一個更多的查詢,雖然...每當我嘗試在onLocationChanged方法吐司...即使應用已關閉,它仍會繼續運行...... !!! – ABC 2014-12-04 12:16:58
您正在尋求更新's'。就像文檔所說的那樣,您需要在一段時間後停止更新。 – 2014-12-04 12:25:54
謝謝@little Child ..非常感謝您的幫助 – ABC 2014-12-05 11:10:45