【问题标题】:Error inflating class fragment with maps使用地图膨胀类片段时出错
【发布时间】:2015-11-22 12:42:03
【问题描述】:

当我运行我的应用程序时,单击指向活动地图的选项后,它以Fatal Exception 结尾。

luizugliano.com.br.lugaresfavoritos.MapsActivity}: android.view.InflateException: Binary XML file line #12: Binary XML file line #12: Error inflating class fragment

我正在使用API23,并已将地图调用更新为SupportMapFragment

我的活动地图

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior">

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:map="http://schemas.android.com/apk/res-auto"
    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"
    tools:context="luizugliano.com.br.lugaresfavoritos.MapsActivity" />

</LinearLayout>

我的 build.gradle 依赖项

compile 'com.google.android.gms:play-services:6.+'

我的地图活动

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.SupportMapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;

public class MapsActivity extends FragmentActivity implements  OnMapReadyCallback {

private GoogleMap mMap;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    // Obtain the SupportMapFragment and get notified when the map is ready to be used.
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);

    Intent intent = getIntent();
    Log.i("InfoLocalizacao", Integer.toString(intent.getIntExtra("InfoLocalizacao", -1)));

}

@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 google-maps android-fragments android-studio


    【解决方案1】:
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">
    
        <fragment
                        xmlns:tools="http://schemas.android.com/tools"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:id="@+id/map"
                        android:name="com.google.android.gms.maps.SupportMapFragment"/>
    
    
    </LinearLayout>
    

    然后

    GoogleMap map;// Global
     map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
    

    【讨论】:

    • 添加FrameLayout作为父布局
    • 为什么要在xml中添加fragment?我觉得应该getFragmentManger在Activity中添加fragment。或者在xml中添加mapview而不是mapfragment。
    • @IntelliJAmiya 我根据他们编辑的响应进行了更改,但仍然返回相同的错误。插入map = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map)).getMap(); 时,我需要删除getMapAsync - map.getMapAsync(this); 的条件。跨度>
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-19
    • 2013-05-28
    相关资源
    最近更新 更多