我似乎找不到以下问题的解决方案。我正在以下Activity上使用以下MapView上的null pointer exception
。
爪哇
public class ActivityMAIN extends FragmentActivity implements LocationListener {
private GoogleMap mMap;
...
mMap = ((MapFragment) getFragmentManager().findFragmentById(R.id.myMapView)).getMap();
//LOGCAT shows Null at above statement only
LatLng sydney = new LatLng(-33.867, 151.206);
mMap.setMyLocationEnabled(true);
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(sydney, 13));
mMap.addMarker(new MarkerOptions()
.title("Sydney")
.snippet("The most populous city in Australia.")
.position(sydney));
XML格式
<fragment
android:id="@+id/myMapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="com.google.android.gms.maps.MapFragment"/>
LogCat
02-02 12:42:51.356: E/AndroidRuntime(4003): Process: com.testing.googlemaps, PID: 4003
02-02 12:42:51.356: E/AndroidRuntime(4003): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.testing.googlemaps/com.testing.googlemaps.ActivityMAIN}: java.lang.NullPointerException
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.os.Handler.dispatchMessage(Handler.java:102)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.os.Looper.loop(Looper.java:136)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.ActivityThread.main(ActivityThread.java:5017)
02-02 12:42:51.356: E/AndroidRuntime(4003): at java.lang.reflect.Method.invokeNative(Native Method)
02-02 12:42:51.356: E/AndroidRuntime(4003): at java.lang.reflect.Method.invoke(Method.java:515)
02-02 12:42:51.356: E/AndroidRuntime(4003): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-02 12:42:51.356: E/AndroidRuntime(4003): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-02 12:42:51.356: E/AndroidRuntime(4003): at dalvik.system.NativeStart.main(Native Method)
02-02 12:42:51.356: E/AndroidRuntime(4003): Caused by: java.lang.NullPointerException
02-02 12:42:51.356: E/AndroidRuntime(4003): at com.testing.googlemaps.ActivityMAIN.onCreate(ActivityMAIN.java:150)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.Activity.performCreate(Activity.java:5231)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-02 12:42:51.356: E/AndroidRuntime(4003): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
最佳答案
问题似乎是在您调用fragment.getMap()方法时,您尝试获取活动创建时的地图,但此时未显示该片段,因此该片段存在但GoogleMap元素不存在,尝试在活动的onResume()方法上执行此操作,或者确保已使用setContentView(...)设置活动的内容