Android:AsyncTask在onCreate中表现得很奇怪

时间:2013-04-24 18:52:28

标签: android android-asynctask

我在onCreate()中以两种不同的方式执行几乎完全相同的代码(除了return语句):

  • 一次进入一个线程(有效)
  • 和AsyncTask中的一次

不知何故,AsyncTask中抛出的Exception甚至无法调试...你知道为什么这段代码在AsyncTask中不起作用,为什么我不能调试de Exception? (它不作为变量出现)

public class TrainingProgressActivity extends ActivityTemplate {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_training_progress);        

        // the not working AsyncTask variant
        new ProgressGetterTask().execute("");

        // the working Thread variant
        new Thread(new Runnable() {
        public void run() {
            try {
                RestTemplate restTemplate = RestTemplateFactory.createRestTemplate();
                FeatureSetCollectionProgress i = restTemplate.postForObject(URLHelper.BASE_PATH+"training/progress", getDevice(), FeatureSetCollectionProgress.class);
                System.out.println(i.getMobilityProgress().get(0).getTransportationMeanType().toString());
            } catch(Exception e) {
                System.out.println("ex");
                e.printStackTrace();
            }
        }}).start();
    }

    /**
     * ProgressGetterTask. (not working ???)
     */
    private class ProgressGetterTask extends AsyncTask<String, FeatureSetCollectionProgress, FeatureSetCollectionProgress> {

        protected FeatureSetCollectionProgress doInBackground(String... urls) {
            try {
                RestTemplate restTemplate = RestTemplateFactory.createRestTemplate();
                return restTemplate.postForObject(URLHelper.BASE_PATH+"training/progress", getDevice(), FeatureSetCollectionProgress.class);
            } catch(Exception e) {
                e.printStackTrace();
                return null; // setting breakpoint here, 'e' cannot be resolved to a variable.
            }
        }

        protected void onPostExecute(FeatureSetCollectionProgress featureSetCollectionProgress) {
            if(featureSetCollectionProgress!=null) {

            } else {

            }
        }
    }
}

0 个答案:

没有答案