2017-05-27 61 views
0

嘿我試圖讓位置服務工作在Android Studio和.getMapAsync(這)不斷崩潰的應用程序。如果任何人都可以告訴我我搞亂了什麼,我會非常感激。如何解決getMapAsync(this)崩潰?

GoogleMap memberMap; 
GoogleApiClient memberAPI; 
LocationRequest memberLocationRequest; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (googleServicesAvailable()) { 

     setContentView(R.layout.activity_main_page); 

     onMapReady(memberMap); 
    } else { 
     Toast.makeText(this, "Google services not available", Toast.LENGTH_LONG); 
     //to be done 
    } 
} 
@Override 
public void onMapReady(GoogleMap googleMap) { 
    memberMap = googleMap; 

    //memberMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
    SupportMapFragment mapFragment =(SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.mapfragment); 
    mapFragment.getMapAsync(this); 





    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if(!locationManager.isProviderEnabled((LocationManager.GPS_PROVIDER))){ 
     buildAlertMessageNoGps(); 
    } 

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      buildGoogleApiClient(); 
      memberMap.setMyLocationEnabled(true); 
     } 
    } 

} 
+0

使用LogCat檢查與您的崩潰相關的Java堆棧跟蹤:https://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this – CommonsWare

回答

0

請致電getMapAsync(this)onCreate()方法。

更新您的代碼如下:

GoogleMap memberMap; 
GoogleApiClient memberAPI; 
LocationRequest memberLocationRequest; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    if (googleServicesAvailable()) { 
     setContentView(R.layout.activity_main_page); 

     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.mapfragment); 
     mapFragment.getMapAsync(this); 
    } else { 
     Toast.makeText(this, "Google services not available", Toast.LENGTH_LONG); 
    } 
} 

@Override 
public void onMapReady(GoogleMap googleMap) { 
    memberMap = googleMap; 

    LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE); 
    if(!locationManager.isProviderEnabled((LocationManager.GPS_PROVIDER))){ 
     buildAlertMessageNoGps(); 
    } 

    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED || ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) { 
      buildGoogleApiClient(); 
      memberMap.setMyLocationEnabled(true); 
     } else { 
      buildGoogleApiClient(); 
      memberMap.setMyLocationEnabled(true); 
     } 
    } 

    // Add markers 
    LatLng sydney = new LatLng(-34, 151); 
    memberMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
    memberMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
} 

................... 
............... 

這裏有一些很好的教程有關GoogleMapLocation API:

  1. Developing an Android app having Google Maps
  2. Google Map Tutorial in Android Studio: How to get current location in Android Google Map
  3. Android Developer’s Guide to the Google Location Services API
相關問題