2017-09-26 57 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”' –