0

我有一个异步任务,我在 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...
}
4

2 回答 2

1

我的 AsyncTask 之前也遇到过类似的情况。问题是我正在捕获一个异常并试图在没有处理程序和可运行的情况下抛出一个吐司。因此,请确保您不要尝试在后台执行过程中对 UIThread 执行任何操作。

于 2013-02-02T04:59:35.413 回答
0

我相信您的问题出在您的.getLatestRssFeed()方法上,因为这些是您的网络连接功能。我看不到您的代码,但我假设您可能正在其中生成一个新线程来访问提要,但您AsyncTask继续并尝试将值分配给liveSpotSilverand Gold,但是它们尚未设置。你需要先解决这个问题,然后看看你是否还有问题。

即使这不是问题,10 次中有 9 次,AsyncTask如果遇到网络错误并且挂起,则不会完成,网络连接是问题。如果您在SilverSpotPriceReaderand中生成新线程Gold...,则不需要这样做,因为您已经在异步完成工作。您可以通过捕获其他人建议的抛出的异常来更好地调试。

于 2013-01-27T20:53:27.820 回答