Android Studio:项目已成功编译,但无法在设备上访问Google Play服务

时间:2013-08-22 19:23:12

标签: java android google-maps android-studio google-play-services

我一直在尝试使用地图和GCM等Google Play服务来编译我的项目。

在关注these instructions之后,我的项目现在成功编译并包含我需要的所有库。 Android Studio还为GCM和地图功能添加了自动完成功能。构建目标设置为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的已知问题吗?我忘了什么吗?根据我从错误日志中读取的内容,看起来libs不是用项目编译的。

2 个答案:

答案 0 :(得分:1)

问题解决了。

首先我加倍检查了JAVA_HOME系统env变量的设置。

设置完成后,我收到编译错误,说明已经添加了一个库:支持v4。

然后我记得我也在使用面向Android工作室的Facebook SDK,他们的build.gradle如下并且过时了:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.4'
    }
}

apply plugin: 'android-library'

dependencies {
    compile files('libs/android-support-v4.jar')
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

解决方案:现在我的脸书build.gradle如下:

buildscript {
    repositories {
        maven { url 'http://repo1.maven.org/maven2' }
    }
    dependencies {
        classpath 'com.android.tools.build:gradle:0.5.+'
    }
}

apply plugin: 'android-library'

dependencies {
    compile 'com.android.support:support-v4:13.0.+'
}

android {
    compileSdkVersion 17
    buildToolsVersion "17.0.0"

    defaultConfig {
        minSdkVersion 7
        targetSdkVersion 16
    }

    sourceSets {
        main {
            manifest.srcFile 'AndroidManifest.xml'
            java.srcDirs = ['src']
            resources.srcDirs = ['src']
            res.srcDirs = ['res']
        }
    }
}

我只是将Gradle插件的版本替换为最近的版本,并使用更新的版本设置新的support-v4依赖项。

tl;博士:如果您正在使用适用于Android Studio的Facebook SDK,请仔细检查FacebookSDK build.gradle是否为最新版。

答案 1 :(得分:0)

可能是你的问题

3.0版本以下不支持片段。但是您使用

Use your minimumSDk version 3.0 are above 3.0

or

Use SupportMapFragment instead of MapFragment

如果您需要教程,请参阅此http://venkoolit.blogspot.in/2013/05/tutorial-for-develop-google-maps-in-api.html