安卓 - 谷歌地图API v2 - SupportMapFragment 错误

13

我正在尝试第一次使用Google Maps API v2使我的应用恢复正常运行。 我在密钥库中为我的应用创建了一个密钥,提取了SHA1哈希值,获取了API密钥,然后在应用程序中执行以下操作...我包含了:

google-play-services.jar

同时导入GooglePlayServices库并将其添加为项目的引用。 在我的Java代码中,我只需加载布局资源。

public class Times extends Activity{
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.map);
    }
}
在我想要实例化的布局(res/layout/map.xml)中,我有以下内容:
<?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:name="com.google.android.gms.maps.SupportMapFragment"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

我在清单文件中也声明了以下内容:

<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="tinyTech.us.ua.busschedule.permission.MAPS_RECEIVE" />

<permission
    android:name="tinyTech.us.ua.busschedule.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

并在清单文件的Application标签中声明:

<meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="My_API_Key" />

当活动加载时,它会在LogCat中崩溃并显示以下错误:

FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
    at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)

我尝试研究了这个问题,但是我无法找到我所需要的。非常感谢任何帮助。


你的活动代码是什么?根据日志“Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment”,你正在尝试将SupportMapFragment转换为非支持版本的Fragment。 - David Snabel-Caunt
是的,这是因为他在活动中使用的基类是Activity而不是FragmentActivity,你可以在我的答案中找到这个信息 :) - Selvin
10个回答

23
如果你使用的是SupportMapFragment,则需要扩展FragmentActivity。如果使用的是MapFragment,则可以扩展Activity

8
我有同样的问题,但我正在扩展FragmentActivity而不是Activity。 - uncle Lem

11

在您的布局中,请使用

<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"/>

取而代之

...    
android:name="com.google.android.gms.maps.SupportMapFragment"
...

7
你必须执行以下操作:
public class MainActivity extends android.support.v4.app.FragmentActivity{

}

如果你能够获取到MapFragment但无法获取到SupportMapFragment,那么原因就在这里! - Aiden Fry

5

5

1
我认为我遇到了同样的问题,似乎在较新的版本中,您需要在清单中添加这一行。
<meta-data
    android:name="com.google.android.gms.version"
    android:value="@integer/google_play_services_version" />

1
我遇到了与Google Play服务相关的问题,我将其包含为库,但问题仍然存在(“找不到com.google.android.gms.maps.suportmapfragment”)。我通过以下步骤解决了这个问题:
1. 进入项目属性。 2. 选择Java Build Path。 3. 然后选择Order and Export选项卡。 4. 确保您勾选了Android DependenciesAndroid Private Libraries
希望这可以帮助你 :)

0
我在清单文件中添加了<uses-library android:name="com.google.android.maps" />。 这对我有用!

0

我尝试了上面所有的方法,但是仍然出现错误。因此,将版本降级至17.1.0

implementation 'com.google.android.gms:play-services-maps:17.1.0'

16.1.0

implementation 'com.google.android.gms:play-services-maps:16.1.0'

对我很有用!


0

我的情况下,我添加:

文件 -> 新建 -> Google -> Google 地图活动

然后我按照这个tutorial进行操作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接