我有一个asynctask,我用来解析ocCreate方法中的JSON和XML数据。问题是异步任务只完成了它的背景"每十次工作一次。当它无法完成它的后台工作时,应用程序因后台进程中未实例化的变量而崩溃,并在后执行中被引用。这很奇怪,因为当AsyncTask完成后台工作时,everythig工作正常。非常感谢任何帮助
我的代码
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...
}
答案 0 :(得分:1)
我之前遇到过与AsyncTask相似的情况。问题是我正在捕获一个异常,试图在没有处理程序和runnable的情况下抛出一个toast。因此,请确保您不要在后台进程中尝试对UIThread执行任何操作。
答案 1 :(得分:0)
我相信您的问题出在您的.getLatestRssFeed()
方法中,因为这些是您的网络连接功能。我无法看到您的代码,但我认为您可能会在访问Feed的内容中生成一个新主题,但您的AsyncTask
会继续并尝试将值分配给liveSpotSilver
和{{ 1}},但他们尚未设置。您需要先修复此问题,然后查看是否仍有问题。
即使这不是问题,10次中有9次,如果您遇到网络错误并且挂起,则<{1}}将无法完成,网络连接是问题。如果要在Gold
和AsyncTask
中生成新线程,则不需要,因为您已经异步进行了工作。您可以通过捕获其他人建议的异常来更好地调试。