1

我知道这是一个重复的问题,但上一篇文章的解决方案对我不起作用。这就是我问这个问题的原因。

我正在尝试在 Android 应用程序中实现 Google Map V2,但出现以下错误

06-26 19:24:51.035: E/AndroidRuntime(7679): FATAL EXCEPTION: main
06-26 19:24:51.035: E/AndroidRuntime(7679): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.titutorial.mapdemo/com.titutorial.mapdemo.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.os.Looper.loop(Looper.java:137)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.ActivityThread.main(ActivityThread.java:4898)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at java.lang.reflect.Method.invoke(Method.java:511)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at dalvik.system.NativeStart.main(Native Method)
06-26 19:24:51.035: E/AndroidRuntime(7679): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:308)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Activity.setContentView(Activity.java:1924)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at com.titutorial.mapdemo.MainActivity.onCreate(MainActivity.java:12)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Activity.performCreate(Activity.java:5206)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
06-26 19:24:51.035: E/AndroidRuntime(7679):     ... 11 more
06-26 19:24:51.035: E/AndroidRuntime(7679): 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
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Fragment.instantiate(Fragment.java:584)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Fragment.instantiate(Fragment.java:552)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Activity.onCreateView(Activity.java:4849)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
06-26 19:24:51.035: E/AndroidRuntime(7679):     ... 20 more
06-26 19:24:51.035: E/AndroidRuntime(7679): Caused by: java.lang.ClassNotFoundException: com.google.android.gms.maps.MapFragment
06-26 19:24:51.035: E/AndroidRuntime(7679):     at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
06-26 19:24:51.035: E/AndroidRuntime(7679):     at android.app.Fragment.instantiate(Fragment.java:574)
06-26 19:24:51.035: E/AndroidRuntime(7679):     ... 23 more

我按照下面的教程,

https://developers.google.com/maps/documentation/android/start#installing_the_google_maps_android_v2_api

我做了以下事情

  1. 安装 Google Play 服务
  2. 在 Google Console 中创建了一个新应用,并获得了 Google Map API 密钥
  3. 在 Eclipse 中创建了新应用,并在 AndroidManifest.xml 中添加了权限和 Google Map API 密钥
  4. 将 Google Play 服务库添加到 App
  5. 将地图添加到应用程序 (main.xml)
  6. 设置目标 API 级别 17

在我的 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"/>

这是我的完整源代码 https://github.com/railskarthi/MapDemo

4

4 回答 4

9

在 API 11+ 之后,我认为您现在必须将您MapFragment的 by替换为SupportMapFragment. 它会是这样的:

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

这里查看更多信息。让我知道你怎么样了。

于 2013-06-26T14:26:22.323 回答
1

我已经从 git 下载了你的项目,检查后我发现你错过了显示地图片段所需的一行。

用以下代码替换您的主要活动代码:-

public class MainActivity extends FragmentActivity {
private GoogleMap map;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map))
            .getMap();
}
于 2013-06-27T09:41:21.613 回答
1

如果您没有在清单文件中为谷歌地图定义 API_KEY,这也可能发生。

它看起来像这样:

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

在Google Developer Console中创建您的密钥

于 2015-03-12T13:19:41.190 回答
0

请在您的清单文件中添加这些行

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

    <!-- Goolge API Key -->
     <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="your Goolge API key" />
于 2015-11-20T07:42:33.997 回答