0

我的 strings.xml 中有一个包含大约 3591 个项目的数组字符串,此列表包含世界上所有的机场。

我正在尝试使用 for 循环在地图上为每个标记添加一个标记。但是,应用程序崩溃。

编辑:根据要求,所有相关代码。我删除了该功能并添加了 AsyncTask:

OnCreate()

String a[] = getResources().getStringArray(R.array.airport_list);
new addAirports().execute(a);

异步任务

private class addAirports extends AsyncTask<String, Void, String[]> {

    @Override
    protected String[] doInBackground(String... a) {
        for(int i=0; i < a.length; i++) {
            MarkerOptions opts = new MarkerOptions();

            Geocoder geocoder = new Geocoder(getBaseContext());
            List<Address> addresses = null;
            try {
                addresses = geocoder.getFromLocationName(a[i], 1);
            } catch (IOException e) {
                e.printStackTrace();
            }

            if (addresses.size() > 0) {
                Address ad = addresses.get(0);

                if (ad != null) {
                    if (ad.getMaxAddressLineIndex() > 0) {
                        String airport = ad.getMaxAddressLineIndex() > 0 ? ad.getAddressLine(0) : "";
                        airport = airport.replace(" Airport", "");
                        opts.icon(BitmapDescriptorFactory.fromBitmap(toBitmap(airport,"airport")));
                        opts.title(ad.getMaxAddressLineIndex() > 0 ? ad.getAddressLine(0) : "");
                        opts.snippet(ad.getCountryName());
                        opts.position(toLatLng(ad));
                        map.addMarker(opts);
                    }
                }
            }
        }
        return a;
    }
}

字符串.xml

这是用于测试目的的缩短列表。它仍然会导致崩溃。

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="airport_list">
        <item>Aberdeen SD (ABR) </item>
        <item>Abilene TX (ABI)</item>
        <item>Adak Island AK (ADK)</item>
        <item>Akiachak AK (KKI)</item>
        <item>Akiak AK (AKI)</item>
        <item>Akron/Canton OH (CAK)</item>
        <item>Akuton AK (KQA)</item>
        <item>Alakanuk AK (AUK)</item>
        <item>Alamogordo NM (ALM)</item>
        <item>Alamosa CO (ALS)</item>
        <item>Albany NY (ALB)</item>
        <item>Albany OR (CVO)</item>
        <item>Albany OR (QWY)</item>
        <item>Albuquerque NM (ABQ)</item>
        <item>Aleknagik AK (WKK)</item>
        <item>Alexandria LA (AEX)</item>
        <item>Allakaket AK (AET)</item>
    </string-array>
</resources>

痕迹

08-19 07:02:02.684: E/AndroidRuntime(27437): FATAL EXCEPTION: AsyncTask #1
08-19 07:02:02.684: E/AndroidRuntime(27437): java.lang.RuntimeException: An error occured while executing doInBackground()
08-19 07:02:02.684: E/AndroidRuntime(27437):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.lang.Thread.run(Thread.java:856)
08-19 07:02:02.684: E/AndroidRuntime(27437): Caused by: java.lang.IllegalStateException: Not on the main thread
08-19 07:02:02.684: E/AndroidRuntime(27437):    at maps.ar.p.b(Unknown Source)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at maps.al.g.b(Unknown Source)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at maps.ag.an.a(Unknown Source)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at bkw.onTransact(SourceFile:167)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at android.os.Binder.transact(Binder.java:326)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addMarker(Unknown Source)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at com.google.android.gms.maps.GoogleMap.addMarker(Unknown Source)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at com.mymaps.Home$addAirports.doInBackground(Home.java:80)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at com.mymaps.Home$addAirports.doInBackground(Home.java:1)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-19 07:02:02.684: E/AndroidRuntime(27437):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-19 07:02:02.684: E/AndroidRuntime(27437):    ... 4 more
4

1 回答 1

0

堆栈跟踪显示

IllegalStateException:不在主线程上

for GoogleMap.addMarker 这个函数应该从 UI 线程调用。这可以通过进行以下更改来完成

final MarkerOptions opts = new MarkerOptions(); 
Handler handler = new Handler(Looper.getMainLooper());

并更换

map.addMarker(opts);

handler.post(new Runnable(){ 
  public void run(){ 
    map.addMarker(opts);
  }
};

或者,Activity.runOnUiThread可以用来代替handler.post

于 2013-08-19T07:24:36.967 回答