2016-09-01 3 views
0

In MainActivity.javaMapFragment null zeigt, die Googlemaps führt zu null

try 
{ 
    supportMapFragment = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map); 
     SupportMapFragment.getMapAsync(this); 
    initializeMap(); 
}catch (Exception e) 
{ 
    e.printStackTrace(); 
} 

onMapReady (GoogleMap googleMap) -Methode in MainActivity.java

public void onMapReady(GoogleMap googleMap) { 
    this.googleMap = googleMap; 
    Context context = this; 

    googleMap.setMyLocationEnabled(true); 

    LocationManager lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE); 
    Location currentLocation = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER); 
    latitudeCurr = currentLocation.getLatitude(); 
    longitudeCurr = currentLocation.getLongitude(); 
    Toast.makeText(MainActivity.this, "lat "+latitudeCurr+" long "+longitudeCurr, Toast.LENGTH_SHORT).show(); 
    googleMap.getUiSettings().setMyLocationButtonEnabled(true); 
    googleMap.getUiSettings().setZoomGesturesEnabled(true); 
    googleMap.getUiSettings().setRotateGesturesEnabled(true); 

    if (Build.VERSION.SDK_INT >= 23 && 
      ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && 
      ContextCompat.checkSelfPermission(context, android.Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 
     return ; 
    } 

Aber MapFragment wird immer null. Ich habe auch versucht mit

mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

Bitte helfen.

Antwort

0

Ich löste es durch diese Verwendung:

SupportMapFragment m = ((SupportMapFragment) getChildFragmentManager() 
       .findFragmentById(R.id.map)); 
m.getMapAsync(this); 

innen Fragment.java wie wir hier das Kind Fragment des gleichen Fragments fordern, und es das Problem gelöst.

Verwandte Themen