Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/195.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android AsyncTask onPostExecute()不是从片段类调用的,而是在活动类中工作_Android_Android Fragments_Android Activity_Android Asynctask - Fatal编程技术网

Android AsyncTask onPostExecute()不是从片段类调用的,而是在活动类中工作

Android AsyncTask onPostExecute()不是从片段类调用的,而是在活动类中工作,android,android-fragments,android-activity,android-asynctask,Android,Android Fragments,Android Activity,Android Asynctask,这是我第一次遇到AsyncTask onPostExecute的问题。我还有其他异步任务在片段中工作。对于这个特定的片段类,AsyncTask doInBackground将完成其执行,但不会调用onPostExecute。我尝试从doInBackground返回数据,并在AsyncTask中分配给一个全局变量。但对于这两种情况,onPostExecute都没有调用 这是我的代码片段 private void downloadFeedsAndTweets() { new Downlo

这是我第一次遇到AsyncTask onPostExecute的问题。我还有其他异步任务在片段中工作。对于这个特定的片段类,AsyncTask doInBackground将完成其执行,但不会调用onPostExecute。我尝试从doInBackground返回数据,并在AsyncTask中分配给一个全局变量。但对于这两种情况,onPostExecute都没有调用

这是我的代码片段

private void downloadFeedsAndTweets() {  
    new DownloadTwitterAsyncTask().execute("ScreenName"); // ScreenName is the proper screen name. 
    new DownloadInstagramFeedAsyncTask().execute("Instagram_URL");  //Instagram_URL is the actual url.
}

public class DownloadInstagramFeedAsyncTask extends AsyncTask<String, Void, Void> {
  List<InstagramData> instagramFeeds = null;

  @Override
  protected Void doInBackground(String... params) {

    try {
        // calls http request to get the feeds
        instagramFeeds = instagramFeedParser(JsonObject);
        Log.d(TAG, "DownloadInstagramFeedAsyncTask instagramFeeds received");
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    // show the feeds in ui using the variable instagramFeeds if != null.
  }

  private List<InstagramData> instagramFeedParser(JSONObject object) throws JSONException, IOException {
    // parse the json and returns the list
    return instagarmFeedList;
  }
}

public class DownloadTwitterAsyncTask extends AsyncTask<String, Void, Void> {
  List<TwitterTweet> twitterTweets = null;

  @Override
  protected Void doInBackground(String... params) {
    if (params.length > 0) {
        // retrieves twitter tweets and assigns to variable twitterTweets.
        twitterTweets = twitterAPI.getTwitterTweets(params[0]);
        Log.e(TAG, "DownloadTwitterAsyncTask tweet received");
    }
    return null;
  }

  @Override
  protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    // update ui with the twitter tweets using variable twitterTweets if != null.
  }
}

如果从活动中调用,则相同的代码也可以工作。在片段中,两个asyncTasks都没有调用onPostExecute。

已解决..在onActivityCreated中,有一个while循环,其条件将在onPostExecute完成后失败。如果我没有错的话,我认为由于这个while循环,onPostExecute保留在主线程中,并等待onActivityCreated完成。

您如何理解onPostExecute没有被调用?返回null?可能是罪魁祸首?尝试使用登录onPostExecuteIs进行调试这里有什么原因不返回doInBackground中的列表吗?将其设置为字段不是线程安全的。工作线程可能已经设置了字段,但ui线程没有看到更改的值。此外,您似乎嵌套了AsyncTask而没有将其设置为静态,这会造成内存泄漏。@hrskrs:在onPostExecute中的超级调用之前,我添加了一个日志,它没有被打印出来。