Android:Google地图示例无效

时间:2013-11-19 13:27:36

标签: android google-maps maps google-maps-api-2

我尝试运行Google地图示例,但是当我选择“基本地图”时它会崩溃。 我做了什么: 文件 - >导入 - > .....->完成 在我的构建路径中添加了v4支持和google play服务.jar。 我不确定是否需要将.jar复制到“libs”文件夹中。 如果我将它们放入“库”中,我需要在构建路径中取消选中它们,否则应用程序甚至不会启动。 这是我的logcat输出:

11-19 07:49:23.129: E/AndroidRuntime(2396): FATAL EXCEPTION: main
11-19 07:49:23.129: E/AndroidRuntime(2396): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapdemo/com.example.mapdemo.BasicMapActivity}: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.os.Looper.loop(Looper.java:137)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.ActivityThread.main(ActivityThread.java:5103)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at java.lang.reflect.Method.invokeNative(Native Method)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at java.lang.reflect.Method.invoke(Method.java:525)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at dalvik.system.NativeStart.main(Native Method)
11-19 07:49:23.129: E/AndroidRuntime(2396): Caused by: android.view.InflateException: Binary XML file line #18: Error inflating class fragment
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.Activity.setContentView(Activity.java:1895)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at com.example.mapdemo.BasicMapActivity.onCreate(BasicMapActivity.java:42)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.Activity.performCreate(Activity.java:5133)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
11-19 07:49:23.129: E/AndroidRuntime(2396):     ... 11 more
11-19 07:49:23.129: E/AndroidRuntime(2396): Caused by: java.lang.NullPointerException: name == null
11-19 07:49:23.129: E/AndroidRuntime(2396):     at java.lang.VMClassLoader.findLoadedClass(Native Method)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at java.lang.ClassLoader.findLoadedClass(ClassLoader.java:354)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at java.lang.ClassLoader.loadClass(ClassLoader.java:491)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.support.v4.app.Fragment.instantiate(Fragment.java:399)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.support.v4.app.Fragment.instantiate(Fragment.java:377)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:277)
11-19 07:49:23.129: E/AndroidRuntime(2396):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
11-19 07:49:23.129: E/AndroidRuntime(2396):     ... 20 more

1 个答案:

答案 0 :(得分:0)

试一试:

请参阅此处,只需使用清单文件中的密钥更改api密钥,然后按照以下步骤操作: 并且确保使用android清单文件中提到的包名称生成api密钥,并且google_play_services_lib项目应仅存在于项目的工作空间中。

清单文件:              

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

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

    <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" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.geeklabs.map.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>

        <meta-data
    android:name="com.google.android.maps.v2.API_KEY"
    android:value="replace with your API key"/>

    </application>

</manifest>

MainActivity.java:

    package com.geeklabs.map;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class MainActivity extends Activity {

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

}

activity_main.xml中:

    <?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"
          android:name="com.google.android.gms.maps.MapFragment"/>

得到这个后告诉我。