向Google地图片段布局添加按钮

时间:2014-04-21 05:15:49

标签: android google-maps android-fragments

我想在地图的左上角添加一个按钮。

这是我的布局:

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


          />

我尝试将片段放在相对布局中,但应用程序崩溃了。

我也看过其他提案,但我无法完成任何工作。

我只需要在地图上放一个按钮。

编辑:

例外文字:

04-20 23:35:18.468: E/AndroidRuntime(7462): FATAL EXCEPTION: main
04-20 23:35:18.468: E/AndroidRuntime(7462): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.solinpromex.vivegrancanaria/com.solinpromex.vivegrancanaria.Empresas_Mapa}: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.os.Looper.loop(Looper.java:137)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at java.lang.reflect.Method.invokeNative(Native Method)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at java.lang.reflect.Method.invoke(Method.java:511)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at dalvik.system.NativeStart.main(Native Method)
04-20 23:35:18.468: E/AndroidRuntime(7462): Caused by: android.view.InflateException: Binary XML file line #7: Error inflating class fragment
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:746)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:270)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.Activity.setContentView(Activity.java:1881)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at com.solinpromex.vivegrancanaria.Empresas_Mapa.onCreate(Empresas_Mapa.java:47)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.Activity.performCreate(Activity.java:5104)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-20 23:35:18.468: E/AndroidRuntime(7462):     ... 11 more
04-20 23:35:18.468: E/AndroidRuntime(7462): Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.Fragment.instantiate(Fragment.java:585)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.Fragment.instantiate(Fragment.java:560)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.app.Activity.onCreateView(Activity.java:4709)
04-20 23:35:18.468: E/AndroidRuntime(7462):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
04-20 23:35:18.468: E/AndroidRuntime(7462):     ... 21 more

1 个答案:

答案 0 :(得分:2)

我打赌这是你的问题:

Caused by: java.lang.ClassCastException: com.google.android.gms.maps.SupportMapFragment cannot be cast to android.app.Fragment

尝试在xml中声明SupportMapFragment而不是MapFragment。并检查您的Activity类是否扩展了FragmentActivity