为什么我无法让谷歌地图V2工作

时间:2013-07-22 05:12:40

标签: android eclipse google-maps

我无法在手机中加载基本的谷歌地图v2 我按照Website进行编码,得到了我的api密钥 但该程序仍然没有工作。这给出了以下错误

错误

07-22 13:02:24.630: E/AndroidRuntime(5323): FATAL EXCEPTION: main
07-22 13:02:24.630: E/AndroidRuntime(5323): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.googlemap/com.example.googlemap.MainActivity}: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.os.Looper.loop(Looper.java:130)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.ActivityThread.main(ActivityThread.java:3691)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at java.lang.reflect.Method.invokeNative(Native Method)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at java.lang.reflect.Method.invoke(Method.java:507)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at dalvik.system.NativeStart.main(Native Method)
07-22 13:02:24.630: E/AndroidRuntime(5323): Caused by: android.view.InflateException: Binary XML file line #17: Error inflating class fragment
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:235)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.Activity.setContentView(Activity.java:1663)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at com.example.googlemap.MainActivity.onCreate(MainActivity.java:11)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615)
07-22 13:02:24.630: E/AndroidRuntime(5323):     ... 11 more
07-22 13:02:24.630: E/AndroidRuntime(5323): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/data/app/com.example.googlemap-1.apk]
07-22 13:02:24.630: E/AndroidRuntime(5323):     at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.createView(LayoutInflater.java:471)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
07-22 13:02:24.630: E/AndroidRuntime(5323):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
07-22 13:02:24.630: E/AndroidRuntime(5323):     ... 20 more


的manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.googlemap"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />
<permission android:name = "com.examaple.googlemap.permission.MAPS_RECEIVE"
            android:protectionLevel="signature"/>

<uses-permission android:name ="com.example.googlemap.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" >

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



        <activity
            android:name="com.example.googlemap.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>
    </application>

</manifest>


布局: - activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/header"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

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


</RelativeLayout>


MainActivity.java

public class MainActivity extends Activity {

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

1 个答案:

答案 0 :(得分:1)

你的min sdk in 8.你应该使用SupportMapFragment

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

您的活动必须延长FragmentActivity

确保您已添加支持库

同时确保导入以下内容

import android.support.v4.app.FragmentActivity;  
import com.google.android.gms.maps.SupportMapFragment;

确保您引用了Google Play服务库项目。

您可以从sdk管理器下载它,并按照以下链接中的建议进行操作。

Importing google-play-service library showing a red X next to this reference android

作为附注,如果您已将ADT更新为rev 22,请确保在java构建路径的oerder导出选项卡中检查Android私有库。清除和构建。如果不需要,请忽略它。

另请确保您已在gogle api控制台的服务标签中启用了Android版谷歌地图。