Android迭代列表-MapView

时间:2014-08-17 23:32:52

标签: java android arrays json android-asynctask

我正在尝试迭代数组列表,但是当我尝试在MapView上创建动态地图标记时,我遇到了错误。当我尝试让for循环迭代allPostMarkers JSON数组中的纬度和经度值时,转换值似乎不适用于此方法的底部。如果您需要我的更多信息,请告诉我,我们很乐意提供更多代码。我仍然确定只是迭代数组列表是问题。

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

1 个答案:

答案 0 :(得分:2)

如果您不在主(UI)线程上,则无法对地图进行更改。因此,你得到了:

java.lang.IllegalStateException: Not on the main thread

异常。

在主线程上执行此操作或创建Handler(稍后将在主线程上运行它):

Handler handler = new Handler(Looper.getMainLooper());
handler.post(new Runnable(){ 
    public void run() {
        // your code that touches the map here
    }
}

在您的情况下,一个简单的方法就是发布每个新标记(这可能效率低下):

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"));
        }
    });
}