Android 膨胀片段类时出错

Android 膨胀片段类时出错,android,google-maps,android-fragments,android-studio,Android,Google Maps,Android Fragments,Android Studio,我找了好几个小时才找到答案,但还是找不到。我正在使用谷歌提供的新Android Studio IDE。UI与Eclipse略有不同,但概念是相同的 我正在尝试为谷歌地图API创建一个地图视图。我正在使用谷歌提供的代码: <?xml version="1.0" encoding="utf-8"?> <fragment xmlns:android="http://schemas.android.com/apk/res/android" android:id=

我找了好几个小时才找到答案,但还是找不到。我正在使用谷歌提供的新Android Studio IDE。UI与Eclipse略有不同,但概念是相同的

我正在尝试为谷歌地图API创建一个地图视图。我正在使用谷歌提供的代码:

    <?xml version="1.0" encoding="utf-8"?>
<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>
下面的清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.proam.app"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="16" />

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

    <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"/>

    <uses-feature
            android:glEsVersion="0x00020000"
            android:required="true"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.proam.app.Main"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
                android:name="com.proam.app.BasicMapActivity"
                android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.proam.app.BASICMAPACTIVITY" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <meta-data
                android:name="com.google.android.maps.v2.API_KEY"
                android:value="AIzaSyDPpk-IXjMIz2XTgbjcFT3M1Eh9K5nLbVU"/>
    </application>

</manifest>

为你的布局试试这个

<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

但是你必须在你的项目中包括谷歌游戏服务库。对不起,我的英文是标题,但我还是把它放进去了。很好,你把它粘贴到了你的书堆里,因为实际错误是:由以下原因引起:java.lang.ClassNotFoundException:com.google.android.gms.maps.MapFragment您是否在项目中包含google地图库JAR文件?我在项目中有google-play-services.JAR,并作为库添加。它与Eclipse不同,所以我不确定如何100%正确地添加它。是的,这就是问题所在,真正的问题是如何在Android StudioOK中正确引用所需的google\u play\u服务jar文件,我会用谷歌搜索它。
06-01 13:22:49.808    1229-1229/com.proam.app                  E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.proam.app/com.proam.app.BasicMapActivity}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
    at android.app.ActivityThread.access$600(ActivityThread.java:130)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:4745)
    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:786)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #7: 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:256)
    at android.app.Activity.setContentView(Activity.java:1867)
    at com.proam.app.BasicMapActivity.onCreate(BasicMapActivity.java:11)
    at android.app.Activity.performCreate(Activity.java:5008)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
    ... 11 more
    Caused by: android.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.app.Fragment.instantiate(Fragment.java:584)
    at android.app.Fragment.instantiate(Fragment.java:552)
    at android.app.Activity.onCreateView(Activity.java:4656)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
    ... 21 more
    Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
    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.app.Fragment.instantiate(Fragment.java:574)
    ... 24 more
<fragment
        android:id="@+id/map"
        android:name="com.google.android.gms.maps.MapFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
package com.proam.app;

import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.SupportMapFragment;

import android.content.Intent;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

public class Main extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button openMap = (Button) findViewById(R.id.openMap);
        openMap.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent map = new Intent("com.proam.app.BASICMAPACTIVITY");
                startActivity(map);

            }
        });
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}