安卓的AsyncTask在外部类部类、在外、AsyncTask

由网友(愿深情不负你)分享简介:我一直工作在一个应用程序,我已经设法获得的AsyncTask 来正常工作时,它是在一个内部类。 I've been working on an app and I've managed to get the AsyncTask to work fine when it's in an inner class. 现在...

我一直工作在一个应用程序,我已经设法获得的AsyncTask 来正常工作时,它是在一个内部类。

I've been working on an app and I've managed to get the AsyncTask to work fine when it's in an inner class.

现在,我重构了code,使的AsyncTask 是在一个单独的它自己的阶级,但我想知道,我怎么杀 ProgressDialog ,并开始一个新的活动,一旦任务顺利完成?我试着开始在 onPostExecute(..)方法一个新的活动,但我知道这是行不通的。

Now, I am refactoring the code so that the AsyncTask is in a separate class of its own, but I am wondering, how do I kill the ProgressDialog and start a new Activity once the task is completed successfully? I've tried starting a new Activity in the onPostExecute(..) method, but I know that won't work.

通过我的UI线程活动中的构造AsyncTask的争论似乎并没有工作:

Passing my UI thread activity as an argument in the constructor for the AsyncTask did not seem to work:

//In UI Thread I had
public class Test101 extends Activity {
    private Button btnLogin;
    private LoginTask mLoginTask;
    private Context context=this;
    private Test101 mTest101;

    mLoginTask=new LoginTask(context,mTest101);
    mLoginTask.execute(null);

    // In the AsyncTask I had
    Activity mParentActivity;

    public LoginTask(Context context,Activity act){
        this.ctx=context;
        this.mParentActivity=act;
    }

    onPostExecute(..){
        mParentActivity.callSomeMethod();
    }

    ...
}

我一直得到一个 NullPointerException异常,也许我失去了一些东西,但对我来说没有工作。

I kept getting a NullPointerException, maybe I'm missing something but that didn't work for me.

推荐答案

而不是传递2参数只能传递一个像这样的:

Instead of passing 2 parameters pass only one like this:

public class Test101 extends Activity {
    private Button btnLogin;
    private LoginTask mLoginTask;

    mLoginTask=new LoginTask(this);
    mLoginTask.execute(); 
    //Instead of sending null put the 1st parameter of the AsyncTask as Void

AsyncTask的code:

AsyncTask Code:

  Activity mParentActivity;

public LoginTask(Activity act){
    this = act;
}

onPostExecute(..){
    ((Test101)mParentActivity).callSomeMethod();
}
阅读全文

相关推荐

最新文章