在Android中为GoogleMap添加一个按钮

时间:2016-10-15 11:39:22

标签: android

Hello guys这是我的Xml代码

我使用相对布局

<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=".MapsActivity" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="com.google.android.gms.maps.MapFragment" />

<Button
    android:id="@+id/add_restaurant"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:text="new mark" />
<Button
    android:id="@+id/remove_restaurant"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/add_restaurant"
    android:text="clear mark" />
    />
<Button
    android:id="@+id/check_map"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_toRightOf="@id/remove_restaurant"
    android:text="just check" />
/>
</RelativeLayout>

它可以像下图一样正确显示

Image

但是当我用手机打开APP时,它就崩溃了

我该如何解决?

Android Studio的错误消息

10-15 20:43:30.100 20922-20922/com.ncku.addmarker E/AndroidRuntime: FATAL     
EXCEPTION: main
                                                                Process: 
com.ncku.addmarker, PID: 20922

java.lang.RuntimeException: Unable to start activity    
ComponentInfo{com.ncku.addmarker/com.ncku.addmarker.MapsActivity}: 
java.lang.NullPointerException: Attempt to invoke virtual method 'void    
  com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2509)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569)

at android.app.ActivityThread.access$900(ActivityThread.java:150)

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399)

at android.os.Handler.dispatchMessage(Handler.java:102)

at android.os.Looper.loop(Looper.java:168)

at android.app.ActivityThread.main(ActivityThread.java:5885)

at java.lang.reflect.Method.invoke(Native Method)

   atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:797)

at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687)
                                                                 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.maps.SupportMapFragment.getMapAsync(com.google.android.gms.maps.OnMapReadyCallback)' on a null object reference

at com.ncku.addmarker.MapsActivity.onCreate(MapsActivity.java:24)

at android.app.Activity.performCreate(Activity.java:6262)

at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1125)

at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2462)

at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2569) 

at android.app.ActivityThread.access$900(ActivityThread.java:150) 

at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1399) 

at android.os.Handler.dispatchMessage(Handler.java:102) 

at android.os.Looper.loop(Looper.java:168)                                                                         
at android.app.ActivityThread.main(ActivityThread.java:5885)                                                                        
at java.lang.reflect.Method.invoke(Native Method)                                                                        
atcom.android.internal.os.ZygoteInit$MethodAndArgsCaller.run     
(ZygoteInit.java:797)                                                                       
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:687) 

2 个答案:

答案 0 :(得分:0)

对于用户界面,您需要更改代码......

如果要在地图片段上添加按钮,则需要添加帧布局。由于框架布局提供了将组件作为图层添加到其他组件的选项。

对于崩溃,请提供崩溃日志。

干杯!!!

答案 1 :(得分:0)

我认为此错误显示是因为您必须不在Google控制台上生成API密钥。如果您尝试加载地图,则必须生成API密钥。尝试此链接并逐步执行..

https://developers.google.com/maps/documentation/android-api/