虽然我最初在片段活动中设置了地图但是它给出了一个我不理解的错误,我检查了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 :(得分: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