【问题标题】:Why can't doInbackground method have empty body?为什么 doInbackground 方法不能有空体?
【发布时间】:2019-10-04 19:37:53
【问题描述】:

我有一个扩展 AsyncTask 的类。我传入一个胆固醇监测器列表并在onPostExecute 中创建片段。但是,doInbackground 不能是空方法,onPostExecute 也不能正确覆盖?

private class CholesterolFragment extends AsyncTask<ArrayList<CholesterolMonitor>, Void, Void>`{

        @Override
        protected Void doInBackground(ArrayList<CholesterolMonitor>... arrayLists) {

        }

        @Override
        protected void onPostExecute(ArrayList<CholesterolMonitor> result){
            cholesterol_monitor= result;
            monitorListFragment = MonitorListFragment.newInstance(cholesterol_monitor);
            getSupportFragmentManager()
                    .beginTransaction()
                    .replace(R.id.fragment_monitor_layout, monitorListFragment)
                    .commit();
        }
    }

在我的 onCreate 中,我传入了胆固醇监测列表:

monitor_list= this.getIntent().getParcelableArrayListExtra("monitorList");

cholesterol_monitor = (ArrayList<CholesterolMonitor>) monitor_list;
CholesterolFragment cholesterolFragment= new CholesterolFragment();
cholesterolFragment.execute(cholesterol_monitor);

【问题讨论】:

  • 如果你的 doInBackground 是空的,你为什么要使用 AsyncTask?
  • 我需要在将 Intent 传递到 Fragment 之前获取它。它一直返回空引用指针,所以我认为它可能在获得意图之前已经创建了片段?

标签: android android-fragments android-intent android-asynctask


【解决方案1】:

使用空的 doInBackground 进行异步任务是没有意义的,此时您不会异步执行任何操作。你也可以直接在主线程上做。

但答案是 doInBackground 返回一个值。所以body可以返回null;但它需要回报。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-13
    • 2013-09-24
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 2015-10-09
    • 1970-01-01
    相关资源
    最近更新 更多