Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
java.lang.NoClassDefFoundError:com.google.android.gms.R$仅当布局文件中存在地图片段时才可设置样式_Android_Google Maps Android Api 2_Android Maps V2 - Fatal编程技术网

java.lang.NoClassDefFoundError:com.google.android.gms.R$仅当布局文件中存在地图片段时才可设置样式

java.lang.NoClassDefFoundError:com.google.android.gms.R$仅当布局文件中存在地图片段时才可设置样式,android,google-maps-android-api-2,android-maps-v2,Android,Google Maps Android Api 2,Android Maps V2,只有当我的布局文件包含一个映射片段时,才会出现此错误,如中所述,其中包括在android:name属性中指定类名等 <fragment android:id="@+id/places_map" android:layout_width="match_parent" android:layout_height="match_parent" class="com.google.android.gms.maps.SupportMapFragment"/> 但是I在以编程方式添加片段时

只有当我的布局文件包含一个映射片段时,才会出现此错误,如中所述,其中包括在android:name属性中指定类名等

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

但是I在以编程方式添加片段时,即在代码中创建SupportMapFragment对象并将其添加到布局文件中的“片段容器”元素时,不会出现此错误

事实上,当以编程方式添加时,它可以正常工作,这让我排除了SO上大多数建议解决方案中建议的“构建路径”错误,并且我也一次又一次地仔细遵循这些步骤,即,将google play服务添加为一个项目,在Properties->Android->project中指定它(我有一个绿色的勾号)以及将google play服务添加为jar等

有没有人有这个问题?特别是“以编程方式添加时工作”和“在布局文件中作为片段元素添加时不工作”问题


关于

我也遇到了同样的问题,在来回奔波了很多次之后,我编译了google_play_服务模块,突然一切都正常了。尝试一下,让我知道在向布局添加映射片段时它是否有效

,希望您使用以下代码“GoogleMap map=((SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map)).getMap();”要创建google map对象,此代码应该在onResume()中或之后。是的,我正是这么做的。但是,当调用setContentView(layout)方法时会发生错误,这是因为布局文件包含一个映射片段。同样的情况也发生在我身上。你修好了吗?我正在使用Android Studio。我什么都试过了,但运气不好。编译时没有错误。