Google地图片段无法充气

时间:2013-04-04 09:34:49

标签: android google-maps fragment

我一直在为我的Android项目添加一个地图,当我的类加载带有地图片段的XML文件时,它崩溃了,我正在学习本教程

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

我正在使用Nexus 7(Jellybean 4.1),所以这应该不是问题,这是我的班级:

package com.app.thetoolstore;

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

public class Tool_map extends Activity{

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


}

这是我的XML:

    <LinearLayout 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:background="#001" >


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



</LinearLayout>

最后我的Stack Trace:

04-04 10:01:35.551: E/AndroidRuntime(14362): FATAL EXCEPTION: main
04-04 10:01:35.551: E/AndroidRuntime(14362): java.lang.RuntimeException:   Unable to     start activity ComponentInfo{com.app.thetoolstore/com.app.thetoolstore.Tool_map}:  android.view.InflateException: Binary XML file line #29: Error inflating class fragment
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1872)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1893)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.ActivityThread.access$1500(ActivityThread.java:135)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1054)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.os.Looper.loop(Looper.java:150)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.ActivityThread.main(ActivityThread.java:4385)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at java.lang.reflect.Method.invokeNative(Native Method)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at java.lang.reflect.Method.invoke(Method.java:507)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at dalvik.system.NativeStart.main(Native Method)
04-04 10:01:35.551: E/AndroidRuntime(14362): Caused by: android.view.InflateException: Binary XML file line #29: Error inflating class fragment
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:581)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:250)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.Activity.setContentView(Activity.java:1742)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at com.app.thetoolstore.Tool_map.onCreate(Tool_map.java:11)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1836)
04-04 10:01:35.551: E/AndroidRuntime(14362):    ... 11 more
04-04 10:01:35.551: E/AndroidRuntime(14362): Caused by: java.lang.ClassNotFoundException: android.view.fragment in loader dalvik.system.PathClassLoader[/system/framework/com.google.android.maps.jar:/data/app/com.app.thetoolstore-2.apk]
04-04 10:01:35.551: E/AndroidRuntime(14362):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at   java.lang.ClassLoader.loadClass(ClassLoader.java:551)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at  java.lang.ClassLoader.loadClass(ClassLoader.java:511)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.createView(LayoutInflater.java:471)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:549)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
04-04 10:01:35.551: E/AndroidRuntime(14362):    at  android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:568)
04-04 10:01:35.551: E/AndroidRuntime(14362):    ... 20 more

更新

我使用本教程https://docs.google.com/document/pub?id=19nQzvKP-CVLd7_VrpwnHfl-AE9fjbJySowONZZtNHzw解决了这个问题,特别是我遇到了 libs 和google-pay服务的问题。

1 个答案:

答案 0 :(得分:3)

很多问题 1)您需要使用FragmentActivity而不是Activity

2)android:name =“com.google.android.gms.maps.MapFragment”替换为android:name =“com.google.android.gms.maps.SupportMapFragment”/&gt;

3)您需要在清单文件中添加密钥并使用Google Android Map API v2

4)参考本教程http://www.vogella.com/articles/AndroidGoogleMaps/article.html