简体   繁体   中英

Google Maps which extends Base Activity can't inflate map fragment

I believe it is possible to setContentView() to a map fragment while extending AppCompatActivity. However I need to extend a Base Activity which extends AppCompatActivity. This means that I need to inflate my map fragment with a container. Unfortunately it throws this exception every time:

Caused by: android.view.InflateException: Binary XML file line #7: Class is not a View com.google.android.gms.maps.SupportMapFragment

Do I have to choose or is there a way to work around this problem?

I am creating an app that needs to access a map while still keep certain functions like the navigation drawer hence I have a Base Activity.

Since you didn't provided code I am just guessing here, but from the error you posted you are probably doing this in your XML:

<com.google.android.gms.maps.SupportMapFragment
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

SupportMapFragment isn't a view, it is fragment, so you should place it using the fragment tag, as following:

<fragment
    class="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

For anyone who still has this problem after migrating to SDK v28, here's something that might help: https://stackoverflow.com/a/34798398/4483494

Essentially, if you have already defined in your application manifest the key "com.google.android.geo.API_KEY", you should delete the key "com.google.android.maps.v2.API_KEY".

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