我正在制作我的第一个Android应用,在初始加载时确定用户的当前位置并在MapView中转到它。我已经将MapView / MapController初始化为AsyncTask以使应用程序感觉更具响应性,但我在doInBackground方法中遇到了RuntimeException。这是我的初始Activity和我正在使用的AsyncTask的代码。
public class MainActivity extends MapActivity {
Location latestLocation, targetLocation;
MapController mapController;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get a reference to the MapView
MapView myMapView = (MapView)findViewById(R.id.myMapView);
MapLoader map = new MapLoader();
try {
map.execute(myMapView).get();
} catch (InterruptedException e) {
e.printStackTrace();
} catch (ExecutionException e) {
e.printStackTrace();
}
mapController = myMapView.getController();
//code which determines current location and animates to it in the Mapview.
}
}
这是我的AsyncTask的代码。
public class MapLoader extends AsyncTask<MapView ,Void, Void>{
MapController mapController;
MapView mapView;
@Override
protected Void doInBackground(MapView... params) {
// Get the Map View’s controller
mapController = params[0].getController();
// Configure the map display options
mapView.setSatellite(true);
mapView.displayZoomControls(false);
// Zoom in
mapController.setZoom(19);
return null;
}
}
我是否应该在AsyncTask中放置用于确定位置的代码?目前,我的应用程序在加载时速度非常慢,并且即使在所有内容都已初始化之后也没有响应。