I'm developing an android application using google maps API V2, the application crashed and I have this error.
06-04 12:26:31.980: E/AndroidRuntime(16726): FATAL EXCEPTION: main
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): at
com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown
Source) 06-04 12:26:31.980: E/AndroidRuntime(16726): at
android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:279)
06-04 12:26:31.980: E/AndroidRuntime(16726): at
android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
06-04 12:26:31.980: E/AndroidRuntime(16726): at
android.view.LayoutInflater.inflate(LayoutInflater.java:466) 06-04
12:26:31.980: E/AndroidRuntime(16726): at
android.view.LayoutInflater.inflate(LayoutInflater.java:396) 06-04
12:26:31.980: E/AndroidRuntime(16726): at
android.view.LayoutInflater.inflate(LayoutInflater.java:352) 06-04
12:26:31.980: E/AndroidRuntime(16726): at
com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:256)
06-04 12:26:31.980: E/AndroidRuntime(16726): at
android.app.Activity.setContentView(Activity.java:1867) 06-04
12:26:31.980: E/AndroidRuntime(16726): at
com.its.android.MainActivity.onCreate(MainActivity.java:29) 06-04
12:26:31.980: E/AndroidRuntime(16726): at
android.app.Activity.performCreate(Activity.java:5008) 06-04
12:26:31.980: E/AndroidRuntime(16726): at
android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-04 12:26:31.980: E/AndroidRuntime(16726): at
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
06-04 12:26:31.980: E/AndroidRuntime(16726): at
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
I can display the map with this code
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();
}
}
but when I try to add a marker with this code, the application crash
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:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
I have added the libraries of Google service.
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>
You are doing it the wrong way:
From the activity layout file you do this:
<?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"/>
So you define a map fragment in your xml file.
From the other hand you are trying to create an instance of the map fragment from code and adding it manualy:
SupportMapFragment fragment = new SupportMapFragment();
getSupportFragmentManager().beginTransaction()
.add(android.R.id.content, fragment).commit();
So basically you are adding the map fragment twice, and they are not connected one to another.
Another thing you should do is to remove this line:
<uses-library android:name="com.google.android.maps" />
it's a API V1 permission and has nothing to do with API V2.