2

我制作了一个使用 Google Android Maps API V2 的应用程序,并且我遵循了 Google 提供的指南中的每一步,但不幸的是它不能正常工作,每次我尝试启动它时它都会崩溃。所以我想知道是什么问题?

MainActivity.java

package com.android.maptest;

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

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

Maptest Manifest.xml

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

    <uses-sdk
        android:minSdkVersion="16"
        android:targetSdkVersion="16" />
    <permission
        android:name="com.android.maptest.permission.MAPS_RECEIVE"
        android:protectionLevel="signature" />

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

    <uses-permission android:name="com.android.maptest.permission.MAPS_RECEIVE" />
    <uses-permission android:name="android.permission.INTERNET" />
    <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" 
        android:debuggable="true" >
        <activity
            android:name="com.android.maptest.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="api_key" />
    </application>

</manifest>

日志猫

03-12 09:46:13.150: D/AndroidRuntime(1547): Shutting down VM

03-12 09:46:13.150: W/dalvikvm(1547): threadid=1: thread exiting with uncaught exception (group=0x410e22a0)

03-12 09:46:13.155: E/AndroidRuntime(1547): FATAL EXCEPTION: main

03-12 09:46:13.155: E/AndroidRuntime(1547): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.android.maptest/com.android.maptest.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at 

android.app.ActivityThread.access$600(ActivityThread.java:140)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.os.Handler.dispatchMessage(Handler.java:99)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.os.Looper.loop(Looper.java:137)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.ActivityThread.main(ActivityThread.java:4898)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at java.lang.reflect.Method.invokeNative(Native Method)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at java.lang.reflect.Method.invoke(Method.java:511)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at dalvik.system.NativeStart.main(Native Method)

03-12 09:46:13.155: E/AndroidRuntime(1547): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Activity.setContentView(Activity.java:1924)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at com.android.maptest.MainActivity.onCreate(MainActivity.java:11)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Activity.performCreate(Activity.java:5206)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)

03-12 09:46:13.155: E/AndroidRuntime(1547):     ... 11 more

03-12 09:46:13.155: E/AndroidRuntime(1547): 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

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Fragment.instantiate(Fragment.java:584)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Fragment.instantiate(Fragment.java:552)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Activity.onCreateView(Activity.java:4849)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)

03-12 09:46:13.155: E/AndroidRuntime(1547):     ... 20 more

03-12 09:46:13.155: E/AndroidRuntime(1547): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment

03-12 09:46:13.155: E/AndroidRuntime(1547):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)

03-12 09:46:13.155: E/AndroidRuntime(1547):     at android.app.Fragment.instantiate(Fragment.java:574)

03-12 09:46:13.155: E/AndroidRuntime(1547):     ... 23 more

03-12 09:51:21.560: I/Process(1547): Sending signal. PID: 1547 SIG: 9
4

4 回答 4

0

对我来说添加:

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

并将xml更改为

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

解决了这个问题。希望你也解决!

于 2013-11-26T11:11:33.953 回答
0

我假设您已经获得Google Maps Android API v2key 并且拥有Google Play services. 如果您尚未安装,请按照以下步骤解决此问题_

  • 第 1 步。您可以在-Google Play 服务安装它的勾选复选框Android SDK Manager下找到它,完成安装后,重新启动 Eclipse。Extras

  • 步骤 2. 将您的 Android SDK 设置文件夹google-play-services_lib下的库 named_ 添加到您的 Android 项目_extras

google服务库路径_

{Your_Android_SDK_direct}\extras\google\google_play_services\libproject\google-play-services_lib 

Ex. Location of this lib on My system_ 
C:\Program Files\Android\android-sdk\extras\google\google_play_services\libproject\google-play-services_lib 

我希望这能解决你的问题。

于 2013-03-12T10:07:49.693 回答
0

看起来您没有将 Google Play 服务库添加到您的项目中。

它位于{Android SDK}\extras\google\google_play_services

将其作为 Android 库项目添加到 Eclipse,然后将其添加到您的项目中。

于 2013-03-12T09:13:44.223 回答
0

这是我写的关于如何将 Google 地图集成到您的应用程序中的博客文章:

创建谷歌地图应用程序

从您的错误来看,您似乎没有正确执行博客指南的步骤 2-3,将 Google Play 服务库添加到您的应用程序中。

如果您仔细按照指南中所写的步骤进行操作,您将获得最终结果,您可以在帖子末尾看到。

于 2013-03-12T09:16:28.780 回答