2013-11-25 56 views
-1

我只是試圖讓谷歌地圖在我的位置按鈕,但應用程序總是崩潰,當我補充一點:setMyLocationEnabled()Google Maps錯誤?

googleMap.setMyLocationEnabled(真)

對顯示着谷歌的方法映射

這裏是我的代碼:

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地圖出現,該方法執行得很好。

回答

1

您的變量mMap爲空。您尚未爲其分配任何對象實例。根據你的佈局xml,你需要給它賦值或初始化它。

0

導致崩潰,因爲你沒有得到地圖,使您的mMap對象爲空當您嘗試訪問它

mMap = getSupportFragmentManager().findFagmentById(r.id.yourfragment).getMap();