Android studio v0.3.2, gradle, google maps v2.0 找不到类“com.google.android.gms.maps.MapFragment

Posted

技术标签:

【中文标题】Android studio v0.3.2, gradle, google maps v2.0 找不到类“com.google.android.gms.maps.MapFragment【英文标题】:Android studio v0.3.2, gradle, google maps v2.0 Didn't find class "com.google.android.gms.maps.MapFragment 【发布时间】:2013-11-14 09:04:00 【问题描述】:

我在使用 Google Maps android v2.0 时遇到问题。因为我尝试了很多选项,我总是得到这个堆栈跟踪:

 1-03 17:39:26.015    2398-2398/com.** E/AndroidRuntime﹕ FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfocom.**/com.**
.MainActivity: android.view.InflateException: Binary XML file line #24: Error inflating class fragment
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: android.view.InflateException: Binary XML file line #24: 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 android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
            at android.app.Activity.setContentView(Activity.java:1881)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.**.MainActivity.onCreate(MainActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     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:409)
            at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
            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 android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
            at android.app.Activity.setContentView(Activity.java:1881)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.**.MainActivity.onCreate(MainActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.android.gms.maps.MapFragment" on path: /data/app/com.**-2.apk
            at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
            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:399)
            at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
            at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
            at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:676)
            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 android.view.LayoutInflater.inflate(LayoutInflater.java:352)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
            at android.app.Activity.setContentView(Activity.java:1881)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:208)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.**.MainActivity.onCreate(MainActivity.java:43)
            at android.app.Activity.performCreate(Activity.java:5104)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5041)
            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:793)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            at dalvik.system.NativeStart.main(Native Method)

我的 build.grade:

    buildscript 
    repositories 
        mavenCentral()
    
    dependencies 
        classpath 'com.android.tools.build:gradle:0.6.+'
    

apply plugin: 'android'

repositories 
    mavenCentral()


android 
    compileSdkVersion 19
    buildToolsVersion "19"

    defaultConfig 
        minSdkVersion 8
        targetSdkVersion 19
    


dependencies 
    compile 'com.google.android.gms:play-services:3.1.36'
//    compile 'com.google.android.gms:play-services:+' //3.2.25
    compile 'com.android.support:support-v4:19.0.+'
    compile 'com.android.support:appcompat-v7:19.0.+'

我的清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-sdk
    android:minSdkVersion="9"
    android:targetSdkVersion="18" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="*****"/>
    <activity
        android:name="com.*.NonLoggedActivity"
        android:configChanges="orientation|keyboardHidden|screenSize"
        android:label="@string/app_name"
        android:theme="@style/FullscreenTheme" >
    </activity>
    <activity
        android:name="com.*.LoginActivity"
        android:label="@string/title_activity_login"
        android:windowSoftInputMode="adjustResize|stateVisible" >

    </activity>
    <activity
        android:name="com.*.MainActivity"
        android:label="@string/title_activity_main" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>
<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="com.google.android.providers.gsf.permission.READ_GSERVICES"/>
<!-- The following two permissions are not required to use
     Google Maps Android API v2, but are recommended. -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

<permission
    android:name="com.*.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />
<uses-permission android:name="com.*.permission.MAPS_RECEIVE" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true"/>
<uses-library
    android:name="com.google.android.maps"
    android:required="false" />

在activity_main.xml中:

    <fragment xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/map"
    android:layout_
    android:layout_
    android:name="com.google.android.gms.maps.MapFragment"/>

SDK 管理器:

和项目结构:

知道有什么问题吗?

【问题讨论】:

您要在什么设备上运行? 模拟器和goclever.com/de/produkte,c1/tablet-pc,c5/aries-785,a149.html 有哪些 Android 版本? 你知道出了什么问题吗? 看起来 google-play-services.jar 类没有放在您的 apk 中,但您的 build.gradle 是正确的。您是否使用gradle installDebug 将其安装在设备上? 【参考方案1】:

将依赖项更改为:

    dependencies 
//    compile 'com.google.android.gms:play-services:3.1.36'
    compile 'com.google.android.gms:play-services:3.2.25' //3.2.25
    compile 'com.android.support:support-v4:19.0.+'
    compile 'com.android.support:appcompat-v7:19.0.+'

并在命令行中写入

sudo . gradlew clean
sudo . gradlew build
sudo . gradlew installDebug

我在 Android Studio 中尝试过,但是当我通过控制台执行此操作时,我的应用程序运行成功。

【讨论】:

【参考方案2】:

其他解决方案:

在 android studio 中重建项目:

Build->rebuild project

【讨论】:

【参考方案3】:

问题是你错过了这个:

    <meta-data android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />

因此,任何解决方案都不会成功,因为被其他问题所掩盖。

【讨论】:

以上是关于Android studio v0.3.2, gradle, google maps v2.0 找不到类“com.google.android.gms.maps.MapFragment的主要内容,如果未能解决你的问题,请参考以下文章

Android Studio 中Gradle配置sonarqube

Android Studio常见问题

Android Studio多渠道打包和代码混淆教程 亲测可用

如何在android studio中处理最近的应用导航按钮?

在 Android Studio 中设置 Flutter 时出现问题

Android Studio 错误:清单合并失败:uses-sdk:minSdkVersion