简体   繁体   中英

Android Google Map Fragment in another Fragment

I can't seem to be able to add google map inside a fragment. Here's my code

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.test.MainActivity">
    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.test.MyMapFragment"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

fragment_mymap.xml

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

MyMapFragment.java

public class MyMapFragment extends Fragment implements OnMapReadyCallback
{
    private GoogleMap mMap;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.fragment_mymap, container, false);

        SupportMapFragment mapFragment = (SupportMapFragment) getFragmentManager().findFragmentById(R.id.google_map);
        mapFragment.getMapAsync(this);

        // Inflate the layout for this fragment
        return v;
    }

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

Yet, I get error of android.view.InflateException: Binary XML file line #8: Error inflating class fragment .

I have tried using MapView in fragment as like in this post , or this one . But the thing is, mapView.getMap() is now deprecated. Though the program threw no error, the map is displayed but it's blank (Just showing Google logo on bottom left corner).

Any suggestion is appreciated.

Yes, 'mapView.getMap()' is now deprecated. Use 'getMapAsync' the object that will be triggered when the map is ready to be used.

For the error ' Error inflating class fragment. ' please check the project build target and minimum target sdk version. Also, don't forget to include your maps permission.

I found this related SO ticket which is related to your issue. There are some solutions offered by other OP and it might solve your issue.

You can also use the Official Google Documentation for Maps Android API, this docs is a quick start on how to create Google maps to an Android app: https://developers.google.com/maps/documentation/android-api/start

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM