1

这是我的 onCreate() 方法。这两条注释的行之前用于启动地图,效果很好。后来我发现 Google 不再使用 LocationClient,而是使用 GoogleAPIClient。所以我改变了它。

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Button send = (Button) findViewById(R.id.button2);

        if(initMap())
        {  
            //mLocationClient = new LocationClient(this,this,this);
            //mLocationClient.connect();
            mGoogleApiClient =new GoogleApiClient.Builder(MainActivity.this)
            .addApi(LocationServices.API)
            .addConnectionCallbacks(MainActivity.this)
            .addOnConnectionFailedListener(MainActivity.this)
            .build();
        }
        else
        {
            Toast.makeText(this,"Map Not Available!",Toast.LENGTH_SHORT).show();
        }
    }

这是 initMap 方法

private boolean initMap(){
        if(mMap==null)
        {
            //making Reference to Map Object
            SupportMapFragment mapFrag = (SupportMapFragment)getSupportFragmentManager().findFragmentById(R.id.map); 
            mMap=mapFrag.getMap();
        }
        return(mMap!=null);
    }

这是 logcat 报告。

12-20 15:45:47.195: E/AndroidRuntime(1457): FATAL EXCEPTION: main
12-20 15:45:47.195: E/AndroidRuntime(1457): Process: com.example.mapsproject, PID: 1457
12-20 15:45:47.195: E/AndroidRuntime(1457): java.lang.NoClassDefFoundError: Failed resolution of: Lcom/google/android/gms/R$styleable;
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.google.android.gms.maps.GoogleMapOptions.createFromAttributes(Unknown Source)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.google.android.gms.maps.SupportMapFragment.onInflate(Unknown Source)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2158)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:297)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:377)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.Activity.setContentView(Activity.java:2144)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.example.mapsproject.MainActivity.onCreate(MainActivity.java:71)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.Activity.performCreate(Activity.java:5933)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.os.Handler.dispatchMessage(Handler.java:102)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.os.Looper.loop(Looper.java:135)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at android.app.ActivityThread.main(ActivityThread.java:5221)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.reflect.Method.invoke(Native Method)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.reflect.Method.invoke(Method.java:372)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-20 15:45:47.195: E/AndroidRuntime(1457): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.mapsproject-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
12-20 15:45:47.195: E/AndroidRuntime(1457):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-20 15:45:47.195: E/AndroidRuntime(1457):     at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
12-20 15:45:47.195: E/AndroidRuntime(1457):     ... 25 more
12-20 15:45:47.195: E/AndroidRuntime(1457):     Suppressed: java.lang.ClassNotFoundException: com.google.android.gms.R$styleable
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.Class.classForName(Native Method)
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
12-20 15:45:47.195: E/AndroidRuntime(1457):         at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
12-20 15:45:47.195: E/AndroidRuntime(1457):         ... 26 more
12-20 15:45:47.195: E/AndroidRuntime(1457):     Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
12-20 15:45:51.501: I/Process(1457): Sending signal. PID: 1457 SIG: 9
4

1 回答 1

1

这是您的活动正常的问题,但 Eclipse 没有在 APK 中包含支持包...如果您查看堆栈跟踪中的错误上方,您会注意到问题的根源:

 Didn't find class "com.google.android.gms.R$styleable" on path: DexPathList[[zip file "/data/app/com.example.mapsproject-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]

所以你可以解压apk,解开它,你会看到兼容性包类没有包括在内。

要解决这个问题

使用 Eclipse

  • 右键单击您的项目,
  • 特性
  • 构建路径
  • 转到导出选项卡。
  • 确保选中“Android 私有库”。
  • 一个干净和重建应该让你走上正轨......

使用安卓工作室

升级到 Android Stuido 0.8.2 后,我遇到了这个问题。我尝试了很多东西,包括

  • 清洁项目
  • 清洁项目(gradle clean)
  • 使缓存无效并重新启动
  • 在 AndroidManifest.xml (.MyActivity) 中取消 Activity 名称的限定

在我重新导入整个项目之前,没有任何效果。

File -> Close Project -> Import Project... -> Choose the same project -> OK

希望有帮助,如果没有,请在下面发表评论。:)

于 2014-12-20T11:23:16.273 回答