我只是想在谷歌地图上启用“我的位置”按钮,但是当我添加这个时,应用程序会一直崩溃:
googleMap.setMyLocationEnabled(true)
显示Google地图的方法
这是我的代码:
package com.example.safezone;
import com.google.android.gms.location.LocationClient;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
public class MainActivity extends FragmentActivity {
GoogleMap mMap;
LatLng myPosition;
LocationClient mLocationClient;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void mapSearch(View v) {
setContentView(R.layout.activity_search);
}
public void myLocation(View v) {
setContentView(R.layout.activity_my_location);
mMap.setMyLocationEnabled(true);
}
}
错误:
11-25 14:42:27.865: E/AndroidRuntime(3521): FATAL EXCEPTION: main
11-25 14:42:27.865: E/AndroidRuntime(3521): java.lang.IllegalStateException: Could not execute method of the activity
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View$1.onClick(View.java:3691)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View.performClick(View.java:4211)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View$PerformClick.run(View.java:17267)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.os.Handler.handleCallback(Handler.java:615)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.os.Handler.dispatchMessage(Handler.java:92)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.os.Looper.loop(Looper.java:137)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.app.ActivityThread.main(ActivityThread.java:4898)
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:42:27.865: E/AndroidRuntime(3521): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-25 14:42:27.865: E/AndroidRuntime(3521): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-25 14:42:27.865: E/AndroidRuntime(3521): at dalvik.system.NativeStart.main(Native Method)
11-25 14:42:27.865: E/AndroidRuntime(3521): Caused by: java.lang.reflect.InvocationTargetException
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invokeNative(Native Method)
11-25 14:42:27.865: E/AndroidRuntime(3521): at java.lang.reflect.Method.invoke(Method.java:511)
11-25 14:42:27.865: E/AndroidRuntime(3521): at android.view.View$1.onClick(View.java:3686)
11-25 14:42:27.865: E/AndroidRuntime(3521): ... 11 more
11-25 14:42:27.865: E/AndroidRuntime(3521): Caused by: java.lang.NullPointerException
11-25 14:42:27.865: E/AndroidRuntime(3521): at com.example.safezone.MainActivity.myLocation(MainActivity.java:31)
11-25 14:42:27.865: E/AndroidRuntime(3521): ... 14 more
如果我不添加setMyLocationEnabled(),并且Google地图出现,该方法就可以正常运行。
答案 0 :(得分:1)
您的变量mMap
为空。您尚未为其分配任何对象实例。根据布局xml,您需要为其赋值或初始化它。
答案 1 :(得分:0)
它崩溃是因为你没有得到地图所以当你试图访问它时你的mMap
对象为空
mMap = getSupportFragmentManager().findFagmentById(r.id.yourfragment).getMap();