-1
我有一個線程循環顯示進度對話框,直到一切都完成。Android AsyncTask -
在這個線程我試圖運行一個類'MapInteractor',它繪製到谷歌地圖的多邊形。但是,我收到了一個錯誤,'不在主線程中'。所以我讀了一下,人們似乎建議使用AsyncTask。 我試過這個,但是我仍然得到一個錯誤。下面顯示縮短的代碼以及錯誤。 如果任何人有任何想法如何解決這個問題,將不勝感激。 我一直把我的頭髮撕掉幾個小時!
mapInteractor = new MapInteractor(this);
fileReader = new FileReader(this);
try {
progress = ProgressDialog.show(this, progressText, "Loading..",
true);
new Thread(new Runnable() {
public void run() {
//does stuff
for (CountryData c : countryData){
try {
c.setLatAndLong(fileReader.getlatlong(c.getCountryName()));
} catch (IOException e) {
e.printStackTrace();
}
mapInteractor.execute(c.getLatAndLong());
}
runOnUiThread(new Runnable() {
@Override
public void run() {
progress.dismiss();
}
});
}
}).start();
} catch (Exception e) {
e.printStackTrace();
}
}
我的地圖交互器類:
public class MapInteractor extends AsyncTask<double[],Integer,Boolean>{
// Google Map
private static GoogleMap googleMap;
private int j = 1;
static int count = 0;
private FragmentActivity fragmentActivity = null;
public MapInteractor(FragmentActivity fragmentActivity) {
this.fragmentActivity = fragmentActivity;
}
@Override
protected Boolean doInBackground(double[]... latLongs) {
boolean complete = false;
count++;
System.out.println(count);
if (googleMap == null) {
googleMap = ((SupportMapFragment) fragmentActivity.getSupportFragmentManager().findFragmentById(R.id.map)).getMap();
}
PolygonOptions polygonOptions = new PolygonOptions();
System.out.println("length of array = " + latLongs[0].length);
for(int i =0;i < latLongs[0].length-1;i++){
if(i == 0){
polygonOptions.add(new LatLng(latLongs[0][j], latLongs[0][i]));
}else{
try{
if(((i*2)+1) <= latLongs.length){
polygonOptions.add(new LatLng(latLongs[0][(i*2)+1], latLongs[0][i*2]));
}
}catch(Exception e){
e.printStackTrace();
};
}
}
if(latLongs.length != 0){
Polygon polygon = googleMap.addPolygon(polygonOptions.strokeColor(Color.TRANSPARENT).fillColor(Color.RED));
complete = true;
}
return complete;
}
我碰到下面的堆棧跟蹤:
04-02 10:48:46.320: E/AndroidRuntime(1704): FATAL EXCEPTION: AsyncTask #1
04-02 10:48:46.320: E/AndroidRuntime(1704): java.lang.RuntimeException: An error occured while executing doInBackground()
04-02 10:48:46.320: E/AndroidRuntime(1704): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.lang.Thread.run(Thread.java:1019)
04-02 10:48:46.320: E/AndroidRuntime(1704): Caused by: java.lang.IllegalStateException: Not on the main thread
04-02 10:48:46.320: E/AndroidRuntime(1704): at maps.k.o.b(Unknown Source)
04-02 10:48:46.320: E/AndroidRuntime(1704): at maps.i.g.b(Unknown Source)
04-02 10:48:46.320: E/AndroidRuntime(1704): at maps.e.al.a(Unknown Source)
04-02 10:48:46.320: E/AndroidRuntime(1704): at etl.onTransact(SourceFile:152)
04-02 10:48:46.320: E/AndroidRuntime(1704): at android.os.Binder.transact(Binder.java:279)
04-02 10:48:46.320: E/AndroidRuntime(1704): at com.google.android.gms.maps.internal.IGoogleMapDelegate$a$a.addPolygon(Unknown Source)
04-02 10:48:46.320: E/AndroidRuntime(1704): at com.google.android.gms.maps.GoogleMap.addPolygon(Unknown Source)
04-02 10:48:46.320: E/AndroidRuntime(1704): at test.threatlevelsystemproject.MapInteractor.doInBackground(MapInteractor.java:60)
04-02 10:48:46.320: E/AndroidRuntime(1704): at test.threatlevelsystemproject.MapInteractor.doInBackground(MapInteractor.java:1)
04-02 10:48:46.320: E/AndroidRuntime(1704): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-02 10:48:46.320: E/AndroidRuntime(1704): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-02 10:48:46.320: E/AndroidRuntime(1704): ... 4 more
感謝。
發表您的完整的日誌 –