Reputation: 199
I'm attempting to iterate over an array list but I am running into errors when I try to create a dynamic map marker on the MapView. Casting the values do not seem to work at the bottom of this method when I try to get the for loop to iterate over both the latitude and longitude values in the allPostMarkers JSON array. Please let me know if you need more information from me and I will be happy to provide more code. I'm still certain just iterating over the array list is the problem.
private JSONArray allPostsMarkers = null;
private ArrayList<HashMap<String, String>> mPostsMarkerList;
public void updateJSONdata() {
// Instantiate the arraylist to contain all the JSON data.
// we are going to use a bunch of key-value pairs, referring
// to the json element name, and the content, for example,
// message it the tag, and "I'm awesome" as the content..
mPostsMarkerList = new ArrayList<HashMap<String, String>>();
// Bro, it's time to power up the J parser
JSONParser jParser = new JSONParser();
// Feed the beast our comments url, and it spits us
// back a JSON object. Boo-yeah Jerome.
JSONObject json = jParser.getJSONFromUrl(MAP_POSTS_URL);
// when parsing JSON stuff, we should probably
// try to catch any exceptions:
try {
// I know I said we would check if "Posts were Avail." (success==1)
// before we tried to read the individual posts, but I lied...
// mComments will tell us how many "posts" or comments are
// available
allPostsMarkers = json.getJSONArray(TAG_POSTS);
// looping through all posts according to the json object returned
for (int i = 0; i < allPostsMarkers.length(); i++) {
JSONObject c = allPostsMarkers.getJSONObject(i);
// gets the content of each tag
String content = c.getString(TAG_MESSAGE);
//String username = c.getString(TAG_USERNAME);
String longitude = c.getString(TAG_LONGITUDE);
String latitude = c.getString(TAG_LATITUDE);
String category = c.getString(TAG_CATEGORY);
System.out.print(latitude);
System.out.print(longitude);
System.out.print(content);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
map.put(TAG_MESSAGE, content);
//map.put(TAG_USERNAME, username);
//map.put(TAG_LONGITUDE, longitude);
//map.put(TAG_LATITUDE, latitude);
map.put(TAG_CATEGORY, category);
// adding HashList to ArrayList
mPostsMarkerList.add(map);
//causing the issues
LatLng shipborough = new LatLng(Float.parseFloat(latitude), Float.parseFloat(longitude));
mMap.addMarker(new MarkerOptions().position(shipborough).title("Post Marker"));
}
LogCat:
08-17 19:31:12.415: E/AndroidRuntime(5468): FATAL EXCEPTION: AsyncTask #2
08-17 19:31:12.415: E/AndroidRuntime(5468): Process: com.rynovation.kline, PID: 5468
08-17 19:31:12.415: E/AndroidRuntime(5468): java.lang.RuntimeException: An error occured while executing doInBackground()
08-17 19:31:12.415: E/AndroidRuntime(5468): at android.os.AsyncTask$3.done(AsyncTask.java:300)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
08-17 19:31:12.415: E/AndroidRuntime(5468): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.lang.Thread.run(Thread.java:841)
08-17 19:31:12.415: E/AndroidRuntime(5468): Caused by: java.lang.IllegalStateException: Not on the main thread
08-17 19:31:12.415: E/AndroidRuntime(5468): at mut.b(Unknown Source)
08-17 19:31:12.415: E/AndroidRuntime(5468): at owa.b(Unknown Source)
08-17 19:31:12.415: E/AndroidRuntime(5468): at oyf.a(Unknown Source)
08-17 19:31:12.415: E/AndroidRuntime(5468): at grl.onTransact(SourceFile:167)
08-17 19:31:12.415: E/AndroidRuntime(5468): at android.os.Binder.transact(Binder.java:361)
08-17 19:31:12.415: E/AndroidRuntime(5468): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
08-17 19:31:12.415: E/AndroidRuntime(5468): at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
08-17 19:31:12.415: E/AndroidRuntime(5468): at com.rynovation.kline.locationViewController.updateJSONdata(locationViewController.java:187)
08-17 19:31:12.415: E/AndroidRuntime(5468): at com.rynovation.kline.locationViewController$LoadMapMarkers.doInBackground(locationViewController.java:224)
08-17 19:31:12.415: E/AndroidRuntime(5468): at com.rynovation.kline.locationViewController$LoadMapMarkers.doInBackground(locationViewController.java:1)
08-17 19:31:12.415: E/AndroidRuntime(5468): at android.os.AsyncTask$2.call(AsyncTask.java:288)
08-17 19:31:12.415: E/AndroidRuntime(5468): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
08-17 19:31:12.415: E/AndroidRuntime(5468): ... 4 more
Upvotes: 0
Views: 111
Reputation: 43504
You cannot do changes to your map if you are not on the main (UI) thread. Therefor you get the:
java.lang.IllegalStateException: Not on the main thread
exception.
Either do it on the main thread or create a Handler
(which will run it later on the main thread):
Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){
public void run() {
// your code that touches the map here
}
}
In your case a simple way to do it would to post each new marker (which could be inefficient):
Handler handler = new Handler(Looper.getMainLooper());
for (int i = 0; i < allPostsMarkers.length(); i++) {
...
final LatLng shipborough = new LatLng(Float.parseFloat(latitude),
Float.parseFloat(longitude));
handler.post(new Runnable() {
public void run() {
mMap.addMarker(new MarkerOptions().position(shipborough)
.title("Post Marker"));
}
});
}
Upvotes: 2