0

我跟隨this tutorial瞭解如何使用googleMaps API。android error:地圖無法解析或不是字段

但MainAntivity.java它給了我一個錯誤,我不知道如何解決:

map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

我得到map cannot be resolved or is not a field但reffers到:d(R.id.map)

任何想法,爲什麼地圖中R. id.map失敗了?

編輯:

MainActivity.java

package com.example.mapsv2; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.Toast; 

import com.google.android.gms.maps.GoogleMap; 
import com.google.android.gms.maps.MapFragment; 
import com.google.android.gms.maps.model.BitmapDescriptorFactory; 
import com.google.android.gms.maps.model.LatLng; 
import com.google.android.gms.maps.model.Marker; 
import com.google.android.gms.maps.model.MarkerOptions; 

public class MainActivity extends Activity { 

    static final LatLng HAMBURG = new LatLng(53.558, 9.927); 
static final LatLng KIEL = new LatLng(53.551, 9.993); 
private GoogleMap map; 

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

     map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap(); 

    if (map!=null){ 
     Marker hamburg = map.addMarker(new MarkerOptions().position(HAMBURG).title("Hamburg")); 
     Marker kiel = map.addMarker(new MarkerOptions() 
      .position(KIEL) 
      .title("Kiel") 
      .snippet("Kiel is cool") 
      .icon(BitmapDescriptorFactory 
       .fromResource(R.drawable.ic_launcher))); 
     } 
} 
} 

的AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?> 

<uses-sdk 
    android:minSdkVersion="12" 
    android:targetSdkVersion="19" /> 

<permission 
    android:name="com.vogella.android.locationapi.maps.permission.MAPS_RECEIVE" 
    android:protectionLevel="signature" /> 

<uses-feature 
    android:glEsVersion="0x00020000" 
    android:required="true" /> 

<uses-permission android:name="com.example.mapsv2.permission.MAPS_RECEIVE" /> 
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" /> 
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" /> 
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <uses-library android:name="com.google.android.maps"/> 
    <activity 
     android:name="com.example.mapsv2.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

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

    <meta-data 
     android:name="com.google.android.maps.v2.API_KEY" 
     android:value="your_apikey" /> 
    <meta-data 
     android:name="com.google.android.gms.version" 
     android:value="@integer/google_play_services_version" /> 
</application> 

+0

足夠的信息來調試或解決問題了嗎? – duggu

+0

把xml和java代碼 –

+0

好吧,我現在用我的java和xml編輯 – Sascuash

回答

1

您是否在XML文件中實現了Fragment?請參閱this blog

+0

Just eddited quetion包含java和xml – Sascuash

0
// try this way,hope this will help you... 

1.Replace this code 
<fragment 
     android:id="@+id/map" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     class="com.google.android.gms.maps.MapFragment" /> 

With this 
<fragment 
     android:id="@+id/map" 
     android:name="com.google.android.gms.maps.MapFragment" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" /> 

2.Add this permission in AndroidManifest file 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 

Note : if you are using latest google-play-services jar for map then you have to also define below meta-data in your AndroidManifest file 
<meta-data android:name="com.google.android.gms.version" 
      android:value="4030500" /> 
+0

其中我在manifest.xml中找不到它 – Sascuash

+0

ok,在activity_main.xml中找到它 – Sascuash

0

此外,您的應用程序應該擴展FragmentActivity。

1

嘗試使用下面的代碼

XML

<fragment 
     android:id="@+id/map" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"   
     class="com.google.android.gms.maps.SupportMapFragment" /> 

的Java

public class MainActivity extends FragmentActivity {  

    GoogleMap googleMap; 

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

     SupportMapFragment supportMapFragment = (SupportMapFragment) 
          getSupportFragmentManager().findFragmentById(R.id.map); 

     // Getting a reference to the map 
     googleMap = supportMapFragment.getMap(); 
} 
}