I am trying to implement Google Maps in android, but whenever I run the app on the emulator it gives me the following Error
:
06-25 02:21:52.890: E/AndroidRuntime(931): FATAL EXCEPTION: main
06-25 02:21:52.890: E/AndroidRuntime(931): java.lang.NoClassDefFoundError: com.google.android.gms.maps.model.LatLng
06-25 02:21:52.890: E/AndroidRuntime(931): at com.example.directions.MainActivity.<clinit>(MainActivity.java:14)
06-25 02:21:52.890: E/AndroidRuntime(931): at java.lang.Class.newInstanceImpl(Native Method)
06-25 02:21:52.890: E/AndroidRuntime(931): at java.lang.Class.newInstance(Class.java:1130)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2128)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.app.ActivityThread.access$600(ActivityThread.java:141)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.os.Handler.dispatchMessage(Handler.java:99)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.os.Looper.loop(Looper.java:137)
06-25 02:21:52.890: E/AndroidRuntime(931): at android.app.ActivityThread.main(ActivityThread.java:5103)
06-25 02:21:52.890: E/AndroidRuntime(931): at java.lang.reflect.Method.invokeNative(Native Method)
06-25 02:21:52.890: E/AndroidRuntime(931): at java.lang.reflect.Method.invoke(Method.java:525)
06-25 02:21:52.890: E/AndroidRuntime(931): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
06-25 02:21:52.890: E/AndroidRuntime(931): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-25 02:21:52.890: E/AndroidRuntime(931): at dalvik.system.NativeStart.main(Native Method)
Following is MainActivity.java
:
package com.example.directions;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import com.google.android.gms.maps.model.MarkerOptions;
import android.os.Bundle;
import android.app.Activity;
import android.widget.Toast;
public class MainActivity extends Activity {
static final LatLng position = new LatLng(21, 57);
private GoogleMap googleMap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
if (googleMap == null) {
googleMap = ((MapFragment) getFragmentManager()
.findFragmentById(R.id.map)).getMap();
}
googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
Marker pos = googleMap.addMarker(new MarkerOptions().position(
position).title("My Position"));
} catch (Exception e) {
e.printStackTrace();
}
}
}
And this the activity_main.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
I have been searching for the solution for a long time. I have already added the Google Play services as a library: File->Import->Existing Android Code into workspace ->Selecting the google play services lib -> Copy projects into workspace
I have also added the project in properties -> Library -> Add-> Google play services lib
.
Kindly help..Thanks in advance.
include below line in android manifest