2

我一直在尝试使用 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 的已知问题吗?我忘记了什么吗?从我从错误日志中读取的内容来看,这些库似乎没有与项目一起编译。

4

2 回答 2

1

问题解决了。

首先,我检查了JAVA_HOME系统环境变量是否已设置。

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

然后我记得我也在使用 Facebook SDK for android studio,并且它们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;dr:如果您使用 facebook SDK for Android Studio,请仔细检查 FacebookSDK 是否build.gradle是最新的。

于 2013-08-23T23:16:08.447 回答
0

可能是你的问题是

3.0以下的版本不支持fragment,但是你习惯了

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

于 2013-08-23T05:17:29.170 回答