2016-08-02 5 views
1

マッププロジェクトを作成してこのコードを実行するだけです。しかし、getMap()エラーがあります。 インターネットからいくつかのサンプルコードを試しましたが、getMap()関数にも同じエラーがあります。コード:getMap()関数のエラー

private GoogleMap mMap; 
    static final LatLng TutorialsPoint = new LatLng(21 , 57); 
    @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_maps); 


    if (mMap == null) { 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 

    try { 
     if (mMap == null) { 

        mMap = ((MapFragment) getFragmentManager(). 
        findFragmentById(R.id.map)).getMap(); 
        //findFragmentById(R.id.map)).getMap(); 
     } 
     mMap.setMapType(GoogleMap.MAP_TYPE_HYBRID); 
     Marker TP = mMap.addMarker(new MarkerOptions(). 
       position(TutorialsPoint).title("TutorialsPoint")); 
    } 
    catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 




@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)); 
} 

これは私のAndroidマニフェストファイルです。

<?xml version="1.0" encoding="utf-8"?> 
    <manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.priya.finalproject"> 

<!-- 
    The ACCESS_COARSE/FINE_LOCATION permissions are not required to use 
    Google Maps Android API v2, but you must specify either coarse or fine 
    location permissions for the 'MyLocation' functionality. 
--> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 

    <!-- 
     The API key for Google Maps-based APIs is defined as a string resource. 
     (See the file "res/values/google_maps_api.xml"). 
     Note that the API key is linked to the encryption key used to sign the APK. 
     You need a different API key for each encryption key, including the release key that is used to 
     sign the APK for publishing. 
     You can define the keys for the debug and release targets in src/debug/ and src/release/. 
    --> 
    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="AIzaSyCBp-ide4ue3DKt-tud4SdzwzR652OSnGY" /> 
    <!-- <meta-data 
     android:name="com.google.android.geo.API_KEY" 
     android:value="@string/google_maps_key" />--> 

    <activity 
     android:name=".MapsActivity" 
     android:label="@string/title_activity_maps"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

+0

どのようなエラーが表示されますか?理想的には完全なスタックを含める必要があります。 – Acapulco

+0

@Acapulco Error Mesgの表示は次のとおりです。 - 12:59:34 AM IllegalStateException:ConnectorServiceRegistryが閉じられているため、DefaultGradleConnector型のオブジェクトのファクトリを見つけることができません。 1:01:55 AM IllegalStateException:ConnectorServiceRegistryが閉じられているため、DefaultGradleConnector型のオブジェクトのファクトリを見つけることができません。 1:02:47 AM IllegalStateException:ConnectorServiceRegistryが閉じられているため、DefaultGradleConnector型のオブジェクトのファクトリを見つけることができません。 – RedEyes

+0

@Acapulcoあなたは私を助けてくれますか? – RedEyes

答えて

2

onMapReady機能がトリガされたとき、あなたのマップにマーカーを追加する必要がありますし、あなたのマップは、あなたのマップを初期化し、

よう getMapAsync関数を呼び出し.just(マーカーなど追加など)を使用する準備ができているとき、この現象が発生
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
      .findFragmentById(R.id.map); 
    mapFragment.getMapAsync(this); 

googleMaponMapReadyの中にあなたに与えられた地図を操作するオブジェクトを使用してください。