2014-12-04 222 views
-1

每當我嘗試獲取地圖時,地圖崩潰,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 

回答

0

getLastKnownLocation()有返回null的可能會導致您的應用程序與NullPointerException崩潰的可能性。

或者,你可以走了過來由Android提供的Location Strategies文檔和使用該處提供的方法:

// Acquire a reference to the system Location Manager 
LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); 
// Define a listener that responds to location updates 
LocationListener locationListener = new LocationListener() { 
    public void onLocationChanged(Location location) { 
     double lat = location.getLatitude(); 
     double lng = location.getLongitude(); 
     // Display a marker on the GoogleMap 
    } 
    public void onStatusChanged(String provider, int status, Bundle extras) {} 
    public void onProviderEnabled(String provider) {} 
    public void onProviderDisabled(String provider) {} 
    }; 
// Register the listener with the Location Manager to receive location updates 
// NOTE : PLEASE ADJUST THE FREQUENCY OF UPDATES PROPERLY 
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener); 

您也可以使用requestSingleUpdate(),如果你只想要一個可以更新到用戶的位置。

+0

我有一個更多的查詢,雖然...每當我嘗試在onLocationChanged方法吐司...即使應用已關閉,它仍會繼續運行...... !!! – ABC 2014-12-04 12:16:58

+0

您正在尋求更新's'。就像文檔所說的那樣,您需要在一段時間後停止更新。 – 2014-12-04 12:25:54

+1

謝謝@little Child ..非常感謝您的幫助 – ABC 2014-12-05 11:10:45

0

你肯定

Location currentLocation=locationManager.getLastKnownLocation(provider); 

不爲空?

你應該實現一個locationChangeListener和使用位置

googlemap.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() { 

@Override 
public void onMyLocationChange(Location location) { 
//Get lattitude and longitude 
double latitude=location.getLatitude(); 
double longitude=location.getLongitude(); 
+0

感謝您的回覆... ITs顯然返回null ...我用你的代碼片段,但顯然ForceClosing應用程序.. – ABC 2014-12-04 06:17:33

0

你有沒有在您的手機上開啓了位置或GPS?另外getLastKnownLocation()返回null值的可能性很高。如果你修復你的日誌併發布它,那會更好。

相關問題