私の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