使用谷歌地图错误膨胀类片段谷歌地图

时间:2013-05-26 05:28:46

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

我是Android开发的新手。我从简单的事情开始,但第一步,只显示一张地图。 以下是我为显示地图所采取的步骤 1)我有google api密钥,并配置android.mainfest。我在元素中添加了权限:MAPS_RECEIVE,ACCESS_COARSE_LOCATION,INTERNET和我的谷歌地图v2 api键。 2)我将google-play-services_lib导入我的工作区。构建它,并添加它对我的项目的引用。 3)因为我使用了android 2.3.3(api 10),所以我使用了SupportMapFragment而不是MapFragment。这是我的布局:

<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"
    tools:context=".ShowMapActivity" >
        <fragment android:id="@+id/map"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                class="com.google.android.gms.maps.SupportMapFragment "/>
</RelativeLayout>

这是我在ShowMapActivity中的代码(它是扩展的FragmentActivity)

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_map);
        if(findViewById(R.id.map)!=null)
        {
            SupportMapFragment fragment = new SupportMapFragment();
            getSupportFragmentManager().beginTransaction().add(R.id.map,fragment).commit();
        }
}

但不幸的是,当我在模拟器中运行应用程序时,我仍然在日志cat中出错:错误地膨胀类片段和com.google.android.gms.maps.MapFragment:确保类名存在,是公共的,并且有一个公共的空构造函数。 编辑: 这是我的日志猫:

05-26 13:52:25.457: E/AndroidRuntime(289): FATAL EXCEPTION: main
05-26 13:52:25.457: E/AndroidRuntime(289): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.com.vogella.android.locationapi.maps/com.vogella.android.locationapi.maps.ShowMapActivity}: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.os.Looper.loop(Looper.java:123)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.ActivityThread.main(ActivityThread.java:3683)
05-26 13:52:25.457: E/AndroidRuntime(289):  at java.lang.reflect.Method.invokeNative(Native Method)
05-26 13:52:25.457: E/AndroidRuntime(289):  at java.lang.reflect.Method.invoke(Method.java:507)
05-26 13:52:25.457: E/AndroidRuntime(289):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
05-26 13:52:25.457: E/AndroidRuntime(289):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
05-26 13:52:25.457: E/AndroidRuntime(289):  at dalvik.system.NativeStart.main(Native Method)
05-26 13:52:25.457: E/AndroidRuntime(289): Caused by: android.view.InflateException: Binary XML file line #6: Error inflating class fragment
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:587)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:623)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
05-26 13:52:25.457: E/AndroidRuntime(289):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:207)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.Activity.setContentView(Activity.java:1657)
05-26 13:52:25.457: E/AndroidRuntime(289):  at com.vogella.android.locationapi.maps.ShowMapActivity.onCreate(ShowMapActivity.java:22)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-26 13:52:25.457: E/AndroidRuntime(289):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
05-26 13:52:25.457: E/AndroidRuntime(289):  ... 11 more
05-26 13:52:25.457: E/AndroidRuntime(289): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.google.android.gms.maps.SupportMapFragment : make sure class name exists, is public, and has an empty constructor that is public

那我怎么解决呢。我错过了哪些步骤或做错了 谢谢你的帮助,对不起大家,因为我的英文写作技巧不好

2 个答案:

答案 0 :(得分:0)

我曾经有过完全相同的问题。问题与...文件路径太长有关。

似乎google-play-services_lib有时会在文件夹结构中嵌套太深。尝试将其复制到其他位置。

答案 1 :(得分:0)

如果您没有找到解决方案,请确保应加载SupportMapFragment的活动扩展android.support.v4.app.FragmentActivity