AsyncTask onPostExecute 永远不会被调用 [英] AsyncTask onPostExecute never gets called

查看:22
本文介绍了AsyncTask onPostExecute 永远不会被调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不确定我做错了什么,但是 onPostExecute 永远不会被调用.

I am not sure what I am doing wrong but onPostExecute never gets called.

  • 创建了一个名为 BaseActivity.java 的基类
  • 从我原来的 Activity 中,我扩展了这个类.
  • 在 BaseActivity 中放置 PostToOpenFeint 类
  • 从我正在做的主要活动的 UI 线程中调用它:

  • Created a base class called BaseActivity.java
  • From my original Activity I extended this class.
  • Placed PostToOpenFeint class inside BaseActivity
  • Called it from the UI thread from the main activity my doing:

new PostToOpenFeint.execute();

onPreExecute()、doInBackground(..) 被触发,但由于某种原因 onPostExecute 从未被调用.

The onPreExecute(), doInBackground(..) gets triggered, but for some reason the onPostExecute never gets called.

提前谢谢您!

戴夫

 private class PostToOpenFeint extends AsyncTask<Void, Void, Void> {
  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  @Override
  protected Void doInBackground(Void... params) {
   // does all the work here
   return null;
  }


  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onPostExecute(java.lang.Object)
   */
  @Override
  protected void onPostExecute(Void result) {
   // TODO Auto-generated method stub
   super.onPostExecute(result);
   Toast.makeText(MainScreen.this, "Done syncing", Toast.LENGTH_LONG).show();
  }

  /*
   * (non-Javadoc)
   * 
   * @see android.os.AsyncTask#onPreExecute()
   */
  @Override
  protected void onPreExecute() {
   // TODO Auto-generated method stub
   super.onPreExecute();
   Toast.makeText(MainScreen.this, "About to sync all your scores", Toast.LENGTH_LONG).show();
  }

<小时>

进一步研究,这是我能够观察到的.例如,如果我拨打此电话:


Looking into it more, this is what I was able to observe. For example if I place this call:

 new PostToOpenFeint.execute();

在 Activity 的 onCreate 之后,一切正常.如果我将此调用放在按钮侦听器中.

right after onCreate of the Activity, then everything works fine. If I place this call say inside a button listener.

settingsButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
   new PostToOpenFeint.execute();
}
});

onPostExecute() 永远不会被调用,不知道我做错了什么.我读到的限制是从 UI 线程调用它,我从 UI 线程调用它.

The onPostExecute() never gets called, not sure what I am doing wrong. The restriction I read was to call this from UI Thread and I am calling it from UI thread.

推荐答案

我刚才遇到了类似的问题.我正在扩展 AsyncTask 并且我的 onPostExecute 方法看起来像:

I had a similiar problem just now. I was extending AsyncTask<Integer,Integer,Integer> and my onPostExecute method looked like:

protected void onPostExecute(int result)

这对我来说似乎没问题,但话说回来,我更像是一个 .NET 人,而不是一个 Java 人.我改成:

This seemed OK to me, but then again I'm more of a .NET guy than a Java guy. I changed it to:

protected void onPostExecute(Integer result)

因为某种原因,Java 认为 intInteger 之间有区别?

because for some reason Java thinks there is a difference between int and Integer?

我认为您的问题是您将返回类型声明为 void 在您的:

I think that your problem is that you're declaring the return type as void in your:

extends<Void,Void,Void>

这意味着没有参数,没有进展,也没有结果.如果你想在完成后执行某事,我认为你需要给它某种值,如整数或布尔值.

That means no params, no progress and no result. If you want to execute something when it's done, I think you need to give it some kind of value like and integer or a boolean.

如果您将扩展更改为:

<Void,Void,Integer>

现在您不传递参数,不发布任何进度,而是返回一个值.更改您的 onPostExecute 方法以接受 Integer:

Now your passing no params, not publishing any progress, but are returning a value. Change your onPostExecute method to accept an Integer:

protected void onPostExecute(Integer result)

然后将 doInBackground 方法中的返回值更改为:

Then change the return in your doInBackground method to something like:

return 1;

然后它应该为您执行.

这篇关于AsyncTask onPostExecute 永远不会被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆