我正在使用谷歌地图 API V2 开发一个 android 应用程序,应用程序崩溃并且我有这个错误。
06-04 12:26:31.980:E/AndroidRuntime(16726):致命异常:主要 06-04 12:26:31.980:E/AndroidRuntime(16726):java.lang.NoClassDefFoundError:com.google.android.gms。 R$styleable 06-04 12:26:31.980: E/AndroidRuntime(16726): at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source) 06-04 12:26:31.980: E/AndroidRuntime( 16726): 在 com.google.android.gms.maps.SupportMapFragment.onInflate(未知来源) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 android.support.v4.app.FragmentActivity.onCreateView( FragmentActivity.java:279) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) 06-04 12:26:31.980: E/AndroidRuntime( 16726): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:466) 06-04 12:26:31.980:E/AndroidRuntime(16726): 在 android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 android.view.LayoutInflater.inflate(LayoutInflater.java :352) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256) 06-04 12:26:31.980: E /AndroidRuntime(16726): 在 android.app.Activity.setContentView(Activity.java:1867) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 com.its.android.MainActivity.onCreate(MainActivity. java:29) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 android.app.Activity.performCreate(Activity.java:5008) 06-04 12:26:31.980: E/AndroidRuntime(16726) : 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 06-04 12:26:31.980: E/AndroidRuntime(16726):在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 06-04 12:26:31.980: E/AndroidRuntime(16726): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
我可以使用此代码显示地图 public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
}}
但是当我尝试使用此代码添加标记时,应用程序崩溃
public class MainActivity extends FragmentActivity {
private GoogleMap mMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpMapIfNeeded();
}
@Override
protected void onResume() {
super.onResume();
setUpMapIfNeeded();
}
private void setUpMapIfNeeded() {
if (mMap == null) {
mMap = ((SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map))
.getMap();
if (mMap != null) {
setUpMap();
}
}
}
private void setUpMap() {
mMap.addMarker(new MarkerOptions().position(new LatLng(0, 0)).title("Marker"));
}
activity_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.SupportMapFragment"/>
我添加了谷歌服务的库
AndroidManifest.xml
<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.its.android"
android:versionCode="1"
android:versionName="1.0" >
<uses-SDK
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<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" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<permission
android:name="com.its.android.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.its.android.permission.MAPS_RECEIVE" />
<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="com.its.android.SplashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.its.android.MainActivity"
android:label="@string/app_name" />
<uses-library android:name="com.google.android.maps" />
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="xxxxx"/>
</application>
</manifest>