无法使用Google服务运行Android项目Google Maps V2
我正在尝试运行一个简单的android项目来显示地图 我已经遵循了谷歌地图中的所有步骤,但eclipse只是阻止了尝试启动该项目 1-我将无法使用Google服务运行Android项目Google Maps V2,android,google-maps-android-api-2,Android,Google Maps Android Api 2,我正在尝试运行一个简单的android项目来显示地图 我已经遵循了谷歌地图中的所有步骤,但eclipse只是阻止了尝试启动该项目 1-我将google-play-services_lib添加到我的工作区。 2-我在工作区复制了它。 3-我创建了一个android项目,其中包括: 清单 活动\u main.xml <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.andro
google-play-services_lib
添加到我的工作区。
2-我在工作区复制了它。
3-我创建了一个android项目,其中包括:
清单
活动\u main.xml
<?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"
android:name="com.google.android.gms.maps.MapFragment"/>
为@PREDACTOR-answer更新了
现在android似乎找不到MainActivity类。我重新制作了项目,重新执行了所有步骤,并在新的工作区中重新制作,但此错误始终出现:
03-08 22:10:38.070:E/AndroidRuntime(17429):java.lang.RuntimeException:无法实例化活动组件信息{com.example.mapasimple/com.example.mapasimple.MainActivity}:java.lang.ClassNotFoundException:com.example.mapasimple.MainActivity
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1884)
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1985)
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.ActivityThread.access$600(ActivityThread.java:127)
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1151)
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.os.Handler.dispatchMessage(Handler.java:99)上
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.os.Looper.loop(Looper.java:137)
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.ActivityThread.main(ActivityThread.java:4447)上
03-08 22:10:38.070:E/AndroidRuntime(17429):位于java.lang.reflect.Method.Invokenactive(本机方法)
03-08 22:10:38.070:E/AndroidRuntime(17429):位于java.lang.reflect.Method.invoke(Method.java:511)
03-08 22:10:38.070:E/AndroidRuntime(17429):位于com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-08 22:10:38.070:E/AndroidRuntime(17429):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-08 22:10:38.070:E/AndroidRuntime(17429):在dalvik.system.NativeStart.main(本机方法)
03-08 22:10:38.070:E/AndroidRuntime(17429):由以下原因引起:java.lang.ClassNotFoundException:com.example.mapasimple.main活动
03-08 22:10:38.070:E/AndroidRuntime(17429):位于dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
03-08 22:10:38.070:E/AndroidRuntime(17429):位于java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-08 22:10:38.070:E/AndroidRuntime(17429):位于java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.Instrumentation.newActivity(Instrumentation.java:1066)上
03-08 22:10:38.070:E/AndroidRuntime(17429):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1875)
03-08 22:10:38.070:E/AndroidRuntime(17429):。。。还有11个
显示用于初始化映射的代码。logcat显示“错误膨胀类片段”,因此在创建映射时出现问题。您的xml文件看起来很好,代码我不知道 但看起来应该是这样的
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager()
.findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(
getActivity().getApplicationContext(),
getActivity().getApplicationContext()
.getResources()
.getString(R.string.map_toast),
Toast.LENGTH_SHORT).show();
}
}
}
这是一段代码,我用来初始化片段内的映射,将其更改为您需要的 Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.mapsss"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="21" />
<permission
android:name="com.example.osman.permission.MAPS_RECEIVE"
android:protectionLevel="signature"/>
<uses-permission android:name="com.example.osman.permission.MAPS_RECEIVE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature
android:glEsVersion="0x00020000"
android:required="true"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"/>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>
</manifest>
主要
main.xml
在主活动中添加此
private GoogleMap mMap;
在onCreate方法中添加
setUpMapIfNeeded();
在onCreate方法下,添加以下内容
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getExtendedMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
}
您的片段布局应该如下所示
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
错误的第一行是指向膨胀视图的第2行。尝试将地图片段放在LinearLayout中,或者尝试xml中的SupportMapFragment…..我尝试了,但出现了相同的错误。是布局还是SupportMapFragment?android:name=“com.google.android.gms.maps.SupportMapFragment”我尝试了布局,让我试试SupportMapFragment我无法运行它。此错误出现在get I add your Code类型MainActivity的方法getSupportFragmentManager()未定义。我已经导入了import com.google.android.gms.maps.SupportMapFragment代码>确保还导入了android.support.v4.app.FragmentActivity;MainActivity的ClassNotFoundException。有什么想法吗?
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="${relativePackage}.${activityClass}" >
<fragment
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment"/>
private GoogleMap mMap;
setUpMapIfNeeded();
private void setUpMapIfNeeded() {
// Do a null check to confirm that we have not already instantiated the
// map.
if (mMap == null) {
// Try to obtain the map from the SupportMapFragment.
mMap = ((SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map)).getExtendedMap();
// Check if we were successful in obtaining the map.
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.setMapType(GoogleMap.MAP_TYPE_SATELLITE);
}
}
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />