2017-04-04 2 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に変更しました。しかし、私のonMapReadyメソッドは、私のgoogleMapオブジェクトがnullなので呼び出されていません。 –

+0

あなたはマニフェストで権限を宣言しましたか? – AwaisMajeed