我想在for循環中聲明谷歌地圖標記。這裏是我的代碼:在for循環中創建谷歌地圖標記
public void latlong() throws JSONException {
JSONArray jsonArrayData = jsonData.getJSONArray("rows");
for (int i = 0; i < jsonArrayData.length(); i++) {
JSONObject c = jsonArrayData.getJSONObject(i);
JSONObject tmpObject = c.getJSONObject("value");
String latString = tmpObject.getString("latitude");
String longString = tmpObject.getString("longitude");
double lat = Double.parseDouble(latString);
double lng = Double.parseDouble(longString);
System.out.println("lat: " + lat + " // long: " + lng);
MarkerOptions place = new MarkerOptions().position(new LatLng(lat ,lng)).title("Hello Maps");
googleMap.addMarker(place);
}
}
我不知道爲什麼我得到一個java.lang.NullPointerException。 感謝您的幫助!
的logcat:
02-12 01:19:02.363 9648-9648/emama.stadtrundgang E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: emama.stadtrundgang, PID: 9648
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.android.gms.maps.model.Marker com.google.android.gms.maps.GoogleMap.addMarker(com.google.android.gms.maps.model.MarkerOptions)' on a null object reference
at emama.stadtrundgang.MapFragment.latlong(MapFragment.java:118)
at emama.stadtrundgang.MapFragment.onCreateView(MapFragment.java:40)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:549)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
發佈您的logcat – Marcus 2015-02-12 00:25:48