java.lang.NoClassDefFoundError:com.google.android.gms.maps.model.LatLng甚至在将google-play-services_lib添加为库项目之后

时间:2015-06-25 07:08:25

标签: java android adt google-maps-android-api-2 eclipse-adt

我正在尝试在Android中实现Google地图,但每当我在模拟器上运行该应用时,它都会给我以下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)

以下是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();
    }
}
}

这是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>

我一直在寻找解决方案。我已将Google Play服务添加为库:File->Import->Existing Android Code into workspace ->Selecting the google play services lib -> Copy projects into workspace

我还在properties -> Library -> Add-> Google play services lib添加了项目。

请帮助..谢谢。

1 个答案:

答案 0 :(得分:1)

在android清单中包含以下行

<uses-library android:required="true" android:name="com.google.android.maps" />