我有一个异步任务,我在 ocCreate 方法期间使用它来解析 JSON 和 XML 数据。问题是异步任务每十次只完成一次它的“后台执行”工作。当它无法完成其后台工作时,应用程序由于未在后台进程中实例化并在执行后引用的变量而崩溃。这很奇怪,因为当 AsyncTask 完成它的后台工作时,一切正常。任何帮助是极大的赞赏
我的代码
private class LoadingData extends AsyncTask<String, Integer, String> {
@Override
protected String doInBackground(String... sUrl) {
//TWITTER
//Parses Twitter XML with no problem...
publishProgress(15,1);
//SPOT GOLD AND SILVER
try{
GoldSpotPriceReader.feed = "http://feed43.com/437361835043234.xml";//Gold
GoldSpotPriceReader.getLatestRssFeed();
livespotgold=GoldSpotPriceReader.spotprice;
storedspotgold=livespotgold;
SilverSpotPriceReader.feed = "http://feed43.com/824383110654537.xml";//Silver
SilverSpotPriceReader.getLatestRssFeed();
livespotsilver=SilverSpotPriceReader.spotprice;
storedspotsilver=livespotsilver;
}catch(NumberFormatException e){
Log.e("Spot Price Initial", "Spot Price Format Error "+e.toString());
}
//AsyncTask Usually Ends Here For No Particular Reason.
//And doesn't finish background work...
publishProgress(30,2);
//More background work...
@Override
protected void onPreExecute() {
super.onPreExecute();
dialog = new ProgressDialog (MyActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
//dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setMessage("Fetching Latest News");
dialog.setIndeterminate(false);
dialog.setMax(100);
dialog.setProgress(0);
dialog.show();
finddealer = (MapView) findViewById(R.id.mvFindDealer);
finddealer.setBuiltInZoomControls(true);
Touchy tango = new Touchy();
overlayList = finddealer.getOverlays();
overlayList.add(tango);
compass = new MyLocationOverlay(MyActivity.this, finddealer);
overlayList.add(compass);
controller = finddealer.getController();
controller.setZoom(11);
lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
lm.requestLocationUpdates(lm.GPS_PROVIDER, 300000, 5000, MyActivity.this);
crit = new Criteria();
towers = lm.getBestProvider(crit, false);
location = lm.getLastKnownLocation(lm.GPS_PROVIDER);
delta = getResources().getDrawable(R.drawable.map_pin_48);
zulu = getResources().getDrawable(R.drawable.ic_launcher);
}
@Override
protected void onProgressUpdate(Integer... progress) {
super.onProgressUpdate(progress);
dialog.setProgress(progress[0]);
switch (progress[1])
{
case 1:
dialog.setMessage("Acquiring Prices...");
break;
case 2:
dialog.setMessage("Acquiring Rates...");
break;
case 3:
dialog.setMessage("Acquiring More Rates...");
break;
case 4:
dialog.setMessage("Pinpointing Your Location...");
break;
case 5:
dialog.setMessage("Locating Nearest Dealers...");
break;
case 6:
dialog.setMessage("Calculating Shortest Routes...");
break;
case 7:
dialog.setMessage("Mapping Dealers...");
break;
case 8:
dialog.setMessage("Finalizing...");
break;
}
}
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
dialog.setProgress(100);
dialog.setMessage("Load Complete.");
dialog.dismiss();
//Other UIthread variables...
}