2017-04-04 1 views
-3

Google지도를 보여주는 내 앱에 활동이 있으며 위치를 선택하는 다른 기능이 있습니다. 이 액티비티는 MapActivity으로 이름 지어지고 FragmentActivity까지 확장됩니다.MapFragment의 NullPointerException; FragmentActivity의 GoogleMap

public class MapActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap googleMap; 

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

    setContentView(R.layout.activity_map); 

    initializeMap(); 
    } 

    private void initializeMap() { 
     if (googleMap == null) { 
      MapFragment mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map); 
      if(mapFragment!=null){ 
       mapFragment.getMapAsync(MapActivity.this); 
      } 

      // check if map is created successfully or not 
      if (null== googleMap) { 
       Toast.makeText(getApplicationContext(), "Sorry! unable to create maps", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    } 

    @Override 
    public void onMapReady(GoogleMap map) { 
     googleMap = map; 
     googleMap.setMyLocationEnabled(true); 
    } 
} 

그러나이 활동이 시작되면 initializeMap 메소드의 mapFragment 객체가 null로 반환되므로 앱이 충돌합니다. getMap()은 사용되지 않으며 getMapAsync() 메서드가 사용되지만이 내 mapFragment 개체 자체는 null이므로이 오류가 발생했습니다.

NullPointerException을 피하기 위해지도 조각을 어떻게 확장합니까? 대한

+0

는 https://developers.google.com/maps/documentation/android-api/start –

답변

0
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback { 

    private GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_maps); 
     // Obtain the SupportMapFragment and get notified when the map is ready to be used. 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 


    /** 
    * Manipulates the map once available. 
    * This callback is triggered when the map is ready to be used. 
    * This is where we can add markers or lines, add listeners or move the camera. In this case, 
    * we just add a marker near Sydney, Australia. 
    * If Google Play services is not installed on the device, the user will be prompted to install 
    * it inside the SupportMapFragment. This method will only be triggered once the user has 
    * installed Google Play services and returned to the app. 
    */ 
    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney and move the camera 
     LatLng sydney = new LatLng(-34, 151); 
     mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney")); 
     mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney)); 
    } 
} 
+0

https://developers.google.com/maps/documentation/android-api/start –

+0

감사합니다 빠른 응답. 나는 링크를 거쳐 MapFragment에서 SupportMapFragment로 코드를 변경했다. 그러나 이제 내 goMap 객체가 null이기 때문에 onMapReady 메서드가 호출되지 않습니다. –

+0

매니페스트에서 권한을 신고하셨습니까? – AwaisMajeed

관련 문제