2017-09-26 59 views
0

雖然我最初在片段活動設置地圖它給出了一個錯誤,我不明白,我檢查正確的名稱XML,但它給了我一個錯誤地圖碎片通貨膨脹錯誤

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.test/com.example.test.MainActivity}: android.view.InflateException: Binary XML file line #4: Error inflating class fragment 

MainActivity.class

public class MainActivity extends FragmentActivity implements OnMapReadyCallback{ 

    GoogleMap mMap; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager() 
       .findFragmentById(R.id.map); 
     mapFragment.getMapAsync(this); 
    } 

    @Override 
    public void onMapReady(GoogleMap googleMap) { 
     mMap = googleMap; 

     // Add a marker in Sydney, Australia, 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)); 
    } 
} 

activity_main.xml中

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

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/map" 
android:name="com.volxom.nearbyalerts.activities.MainActivity" 
android:layout_width="match_parent" 
android:layout_height="match_parent" /> 
+0

您可以發佈您的XML – UltimateDevil

+0

是肯定即時消息 –

+0

@MuhammadAli顯示您的xml。 –

回答

4

您必須在名稱屬性中使用SupportMapFragment

class="com.google.android.gms.maps.SupportMapFragment" 

你的XML應該是這樣的:

<fragment xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:id="@+id/map" 
class="com.google.android.gms.maps.SupportMapFragment" 
android:layout_width="match_parent" 
android:layout_height="match_parent" /> 

你應該在你的清單有這樣的:

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

參考:https://developers.google.com/android/reference/com/google/android/gms/maps/SupportMapFragment

+0

同樣的問題@Bhuvanesh Bs –

+0

再次出現同樣的問題 –

+0

@MuhammadAli嘗試使用'class =「com.google.android.gms.maps.SupportMapFragment」' –