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

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

我正在尝试在 android 中实现 Google map ,但每当我在模拟器上运行该应用程序时,它都会给我以下错误:

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 中添加了该项目。

请帮助..提前致谢。

最佳答案

在 android list 中包含以下行

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

关于java.lang.NoClassDefFoundError : com. google.android.gms.maps.model.LatLng 即使在将 google-play-services_lib 添加为库项目之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31043506/

相关文章:

java - 如何在 Java 中调整 AWT 多边形的大小

java - 使用 Gradle 执行单个 JUnit5 测试

android webview 占满全屏

android - Eclipse Indigo Android ADT - 安装正常,首选项和新项目中没有 "Android"选项

java - 一个循环中的不同扫描仪数据类型。该怎么办?

java - 小米设备允许应用程序在后台运行时弹出窗口

android - 制作点击 Activity

java - 从 Windows 8 中提取 adt-bundle-windows-x86_64-20140702 时出现此错误

eclipse - 无法安装 eclipse adt 插件

java - 在 GlassFish 4.1 中升级 Jackson