我一直在尝试使用 Google Play 服务(例如 Maps 和 GCM)来编译我的项目。
按照这些说明进行操作后,我的项目现在可以成功编译并包含我需要的所有库。Android Studio 还为 GCM 和 Maps 功能添加了自动完成功能。构建目标设置为Google APIs 18
但是,当我打算启动我的应用程序时,它会在命中时崩溃
if(GooglePlayServicesUtil.isGooglePlayServicesAvailable(this) != ConnectionResult.SUCCESS){
setContentView(R.layout.error_no_play);
return;
或者当它碰到地图片段时
<fragment
android:id="@+id/map"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:layout="@layout/fragment_gmap"
class="com.google.android.gms.maps.MapFragment"/>
抛出的错误是:
检查 GooglePlayServices 是否可用时:
java.lang.NoClassDefFoundError: com.google.android.gms.common.GooglePlayServicesUtil
对于片段:
…………
08-22 20:17:56.565 20356-20356/<MY_APP> E/AndroidRuntime: FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{<MY_APP>/<MY_APP>.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2178) at android.app.ActivityThread.access$700(ActivityThread.java:141) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1271) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:137) at android.app.ActivityThread.main(ActivityThread.java:5118) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:511) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555) at dalvik.system.NativeStart.main(Native Method) Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) at android.view.LayoutInflater.rInflate(LayoutInflater.java:746) at android.view.LayoutInflater.inflate(LayoutInflater.java:489) at android.view.LayoutInflater.inflate(LayoutInflater.java:396) at <MY_APP>.SearchFragment.newTab(SearchFragment.java:71) at <MY_APP>.SearchFragment.setupTabs(SearchFragment.java:54) at <MY_APP>.SearchFragment.onCreateView(SearchFragment.java:28) at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088) at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163) at android.app.Activity.performStart(Activity.java:5068) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2113) ... 11 more Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.MapFragment: make sure class name exists, is public, and has an empty constructor that is public at android.support.v4.app.Fragment.instantiate(Fragment.java:401) at android.support.v4.app.Fragment.instantiate(Fragment.java:369) at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:272) at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676) ... 26 more Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/<MY_APP>-2.apk at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) at java.lang.ClassLoader.loadClass(ClassLoader.java:501) at java.lang.ClassLoader.loadClass(ClassLoader.java:461) at android.support.v4.app.Fragment.instantiate(Fragment.java:391) ... 29 more
清单文件如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="<MyApp>"
android:versionCode="1"
android:versionName="1.0" >
<!-- Permissions required for GMap and Facebook to work -->
<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="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="com.google.android.c2dm.permission.RECEIVE" />
<permission android:name="<MyApp>.permission.C2D_MESSAGE"
android:protectionLevel="signature" />
<uses-permission android:name="<MyApp>.permission.C2D_MESSAGE" />
<!-- Require OpenGL ES2 for GMap -->
<uses-feature android:glEsVersion="0x00020000"
android:required="true" />
<!-- Because this app is using the GCM library to send messages, the min SDK cannot be lower
than 8. Using SDK11-->
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/MyTheme" >
<receiver
android:name=".MyPushReceiver"
android:permission="com.google.android.gcm.c2dm.permission.SEND" >
<intent-filter>
<action android:name="com.google.android.c2dm.intent.RECEIVE" />
<category android:name="<MyApp>" />
</intent-filter>
</receiver>
<activity
android:name="<MyApp>.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Facebook specific activities -->
<activity android:name="com.facebook.LoginActivity" />
<!-- GMap API key -->
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="<MY_KEY>" />
<!-- Facebook App ID -->
<meta-data android:name="com.facebook.sdk.ApplicationId" android:value="@string/app_id" />
</application>
</manifest>
我已经处理这个问题1个月了,我仍然没有办法处理它。这是 AndroidStudio 的已知问题吗?我忘记了什么吗?从我从错误日志中读取的内容来看,这些库似乎没有与项目一起编译。