error using maps in fragment

2019-09-03 04:04发布

问题:

i am having this error when i try to open my fragment "activityMapa", apparently can't inflate my xml file:

11-15 21:58:39.769: E/AndroidRuntime(2432): FATAL EXCEPTION: main
11-15 21:58:39.769: E/AndroidRuntime(2432): android.view.InflateException: Binary XML file line #2: Error inflating class fragment
11-15 21:58:39.769: E/AndroidRuntime(2432):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
11-15 21:58:39.769: E/AndroidRuntime(2432):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
11-15 21:58:39.769: E/AndroidRuntime(2432):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
11-15 21:58:39.769: E/AndroidRuntime(2432):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
11-15 21:58:39.769: E/AndroidRuntime(2432):     at com.example.despegarteproject.ActivityMapa.onCreateView(ActivityMapa.java:43)
11-15 21:58:39.769: E/AndroidRuntime(2432):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:832)
11-15 21:58:39.769: E/AndroidRuntime(2432):     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1036)

this is what i am doing in my onCreateView :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.activity_mapa, null);


    context = getActivity();

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

    mapa.setOnMarkerClickListener(new OnMarkerClickListener() {

        public boolean onMarkerClick(Marker marker) {
            Toast.makeText(context, "Marcador pulsado:\n" + marker.getTitle(), Toast.LENGTH_SHORT).show();
            return false;
        }
    });

    moveTo(40.417325, -3.683081);
    addMarker(40.417325, -3.683081, "Madrid, acá");

    return view;
}

and my xml file "acitivty_mapa":

<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.SupportMapFragment"/>

回答1:

Create a class that derives from SupportMapFragment, and you won't need to inflate a layout.

public class MyMapFragment extends SupportMapFragment {

    private GoogleMap map;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    @Override
    public void onResume() {
        super.onResume();
        map = getMap();
    }

Then, when you need to use this in your activity's layout, you'd reference your class within the activity's layout xml

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.example.despegarteproject.MyMapFragment" />


标签: android map